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 Request: "Don't care" parameters #3449

Closed
togakangaroo opened this issue Apr 18, 2014 · 13 comments
Closed

Feature Request: "Don't care" parameters #3449

togakangaroo opened this issue Apr 18, 2014 · 13 comments

Comments

@togakangaroo
Copy link

I'd like to submit this feature for discussion and if I get positive feedback might take a crack at implementing it.

Occasionally I get into situations where I don't care about a particular parameter but I have to include it because I use a positional parameter to its right. The most obvious example is receiving data into a jquery event callback

$element.on 'click', (e, thingIActuallyCareAbout) -> ...

I don't care about e at all but have to include it.

In pattern-matching languages there's the catchall _ pattern, so I would just use that, but I can't repeat it in a parameter list and with the prevalence of underscore/lodash it would actually be more confusing.

I propose a new symbol that you can use in parameter lists to indicate you don't care something like

doStuffAndCall (*, *, data) -> ....

It could generate something like

doStuffAndCall(function(__1, __2, data) { ... })

What does everyone think?

@davidchambers
Copy link
Contributor

Perhaps a coding convention is sufficient:

doStuffAndCall (x, xx, data) ->
  ...

Another option:

doStuffAndCall ->
  data = arguments[2]
  ...

@vendethiel
Copy link
Collaborator

Duplicate parameter names (as _) were shot down in a previous ticket.

#1002

@michaelficarra
Copy link
Collaborator

The problem still exists. You're forced to name parameters you won't reference in the function body.

@lydell
Copy link
Collaborator

lydell commented Apr 20, 2014

There’s always the old “destructure into nothing” trick:

doStuffAndCall ([], [], data) ->
  ...
# alternatively:
doStuffAndCall ({}, {}, data) ->
  ...

@michaelficarra said in #1558 (comment):

A parameter list its just an implicit positional destructuring assignment of arguments. It should support all of the LHSs assignment does.

Which seems to be true, and sane. Using ? as a placeholder more or less anywhere has been talked about before (including the following comment), such as for ?, index in array. The “destructure into nothing” trick works there, too: for [], index in array.

So, IMO, this issue could be generalized into adding a single-item placeholder syntax for destructuring in general, which has been talked about before. The latest discussion being #3268 (comment), I believe. (Using the “destructure into nothing” trick feels like a hack to me, isn’t very intuitive, is a bit ugly and hasn’t got an ideal compilation.)

@michaelficarra
Copy link
Collaborator

Ah, I had forgotten about that. Thanks, @lydell. Looks like we already decided that [] or {} would be the accepted standard in #3268.

@xixixao
Copy link
Contributor

xixixao commented Apr 30, 2014

Hmm, I see I didn't get around to it. But clearly ? is not a good placeholder and I think we're out of characters (_ obviously being unusable given its popularity as valid identifier).

@satyr
Copy link
Collaborator

satyr commented Apr 30, 2014

we're out of characters

We don't need a character for this. Just allow: (, , data) ->

@davidchambers
Copy link
Contributor

Great idea, @satyr. It's clear to me what this means, as it resembles JavaScript's [,,,42].

@lydell
Copy link
Collaborator

lydell commented Apr 30, 2014

I thought about that, too, some time ago. I really like it. The reason I didn’t propose it was that I couldn’t make up my mind about when newlines are used instead of commas.

[uninteresting, interesting] = foo
[
  uninteresting
  interesting
] = foo

[ , interesting] = foo
[
  # ???
  interesting
] = foo

Should a comma be required in that last case?

[
  ,
  uninteresting
] = foo

@vendethiel
Copy link
Collaborator

Another funny one is : should [1,,] be the same as [1,] ? (array trailing commas)

@michaelficarra
Copy link
Collaborator

I was just going to bring that up, @Nami-Doc. Array and object literals (in CS and ES5) have both holes and elisions. If parameter lists are allowed to have holes, do they also have elisions? Basically, what is the arity of (a, b,) ->?

@lydell
Copy link
Collaborator

lydell commented Apr 30, 2014

Another problem is that [a,]=b is currently allowed. (It is equivalent to [a]=b.)

@satyr
Copy link
Collaborator

satyr commented Apr 30, 2014

when newlines are used instead of commas

Should a comma be required in that last case?

It'd have to.

In case that feels too awkward, we could also allow null as placeholder:

[
  null
  interesting
] = foo

what is the arity of (a, b,) ->?

Should be same as [a, b,].length.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants