Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Don't care/anonymous parameter #5586

Closed
qc00 opened this issue Nov 10, 2015 · 9 comments
Closed

Feature: Don't care/anonymous parameter #5586

qc00 opened this issue Nov 10, 2015 · 9 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@qc00
Copy link

qc00 commented Nov 10, 2015

Many functional languages have a don't care parameter/variable that holds values not used by programs.

For example, a function like Array.prototype.forEach supplies three arguments to the callback. If the code only needs the second argument (index), it will still have to supply a name for the first one. The programmer can use a don't care parameter as the name of the first argument instead. This improves readability and avoids problem of accidentally shadowing.

An additional keyword/comment may have to be introduced to define which string is used for this purpose as the traditional "_" happens to be used by the popular underscore library.

This can also be extended to destructuring code.

@qc00 qc00 changed the title Don't care/anonymous parameter Feature: Don't care/anonymous parameter Nov 10, 2015
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Nov 10, 2015
@RyanCavanaugh
Copy link
Member

Just thinking aloud -- would arr.forEach(( , index) => console.log(index)); look too much like a typo?

@sandersn
Copy link
Member

I think so. Also, when commas stack up they get hard to count.

Does the already-supported workaround of _, __, __, ... suffice?

@FabianLauer
Copy link

I agree with @sandersn. How about arr.forEach((void, index) => console.log(index));?
Just the first thing that came to mind. Correct me if I'm wrong, but I think this doesn't lead to a conflict with the ES spec as it would be a part of the type system.

@coreh
Copy link

coreh commented Dec 3, 2015

Does the already-supported workaround of _, __, __, ... suffice?

Maybe _ could be special cased, so that you can use it multiple times, then __ and ___ wouldn't be needed.

(_, __, x, ____) => x + 1

Would become

(_, _, x, _) => x + 1

@LiamGoodacre
Copy link

How about ?? For example: (?, x, ?) => x + 1.
Given that _ is a valid identifier name, I don't believe it is appropriate to use here.

@DanielRosenwasser
Copy link
Member

You could just use an empty binding pattern like[] or {}.

arr.forEach(({}, index) => console.log(index));

arr.forEach(([], index) => console.log(index));

Since they are not disallowed (see tc39/ecma262#97), I'm inclined to close this unless there's a problem with that workaround.

@LiamGoodacre
Copy link

@DanielRosenwasser nice 👍

@qc00
Copy link
Author

qc00 commented Dec 20, 2015

@DanielRosenwasser Sounds perfect. Would be nice if this special use case is documented though.

@FabianLauer
Copy link

@DanielRosenwasser Great, thanks.

@RyanCavanaugh RyanCavanaugh added Out of Scope This idea sits outside of the TypeScript language design constraints and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Dec 21, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants