You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
I agree with Nathan Shively-Sanders (@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.
Activity
[-]Don't care/anonymous parameter[/-][+]Feature: Don't care/anonymous parameter[/+]RyanCavanaugh commentedon Nov 10, 2015
Just thinking aloud -- would
arr.forEach(( , index) => console.log(index));look too much like a typo?sandersn commentedon Nov 10, 2015
I think so. Also, when commas stack up they get hard to count.
Does the already-supported workaround of
_,__,__, ... suffice?FabianLauer commentedon Nov 10, 2015
I agree with Nathan Shively-Sanders (@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 commentedon Dec 3, 2015
Maybe
_could be special cased, so that you can use it multiple times, then__and___wouldn't be needed.Would become
LiamGoodacre commentedon Dec 20, 2015
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 commentedon Dec 20, 2015
You could just use an empty binding pattern like
[]or{}.Since they are not disallowed (see tc39/ecma262#97), I'm inclined to close this unless there's a problem with that workaround.
LiamGoodacre commentedon Dec 20, 2015
Daniel Rosenwasser (@DanielRosenwasser) nice 👍
qc00 commentedon Dec 20, 2015
Daniel Rosenwasser (@DanielRosenwasser) Sounds perfect. Would be nice if this special use case is documented though.
FabianLauer commentedon Dec 21, 2015
Daniel Rosenwasser (@DanielRosenwasser) Great, thanks.