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

optional args in destructuring #277

Closed
weepy opened this issue Mar 23, 2010 · 14 comments
Closed

optional args in destructuring #277

weepy opened this issue Mar 23, 2010 · 14 comments

Comments

@weepy
Copy link

weepy commented Mar 23, 2010

No idea if this is easy/hard/worth it, but optional args in destructuring arrays might be nice:

[,,x] : [1,2,3]
ok x is 3
@zmthy
Copy link
Collaborator

zmthy commented Mar 23, 2010

I brought this up a while back, and it was decided against because the syntax was considered error prone. Perhaps [null, null, x]: [1, 2, 3]?

@beastaugh
Copy link

Haskell uses the underscore (e.g. below), but of course that's a valid identifier in JavaScript.

f _ 0 = 1
f x n = x * n

@zmthy
Copy link
Collaborator

zmthy commented Mar 23, 2010

How about ? then? By itself it has no meaning.
[?, ?, x]: [1, 2, 3]
- also makes sense.

@matehat
Copy link
Contributor

matehat commented Mar 23, 2010

If we can settle on a symbol that means "empty" here, I can think of many other situations were that could be used as a kind of placeholder to improve functionalities

@weepy
Copy link
Author

weepy commented Mar 23, 2010

? seems the obvious choice.

@matehat
Copy link
Contributor

matehat commented Mar 23, 2010

I agree it's a great choice. Also, it'd be a lot easier to implement than - since it interferes with almost nothing

@weepy
Copy link
Author

weepy commented Mar 23, 2010

where else do you see opportunities for an empty symbol ?

@zmthy
Copy link
Collaborator

zmthy commented Mar 23, 2010

It could be a direct reference to undefined. Imagine calling a function where passing null means "nothing" as opposed to "ignore this argument":
call_me(5, ?, ?, null, 6);
It's unlikely that situations like this would arise, though it makes more sense semantically.
Usage like this is really outside the scope of this discussion.

@matehat
Copy link
Contributor

matehat commented Mar 23, 2010

Well, in parallel with another discussion on defer keyword for continuation, ? could be used to specify alternative position of implicit callback, for example :

double: (x, callback, error_callback) ->
  process.nextTick -> 
    if Number(x) isnt NaN then callback(2*x) else error_callback(x)

x: "Hello!"
doubled: defer double x, ?, (x) -> puts "$x is not a number!"
puts "$x doubles into $doubled"

Here, the ? could specify a placeholder for the implicit callback. Also, it could make function binding more flexible :

calc: (x, y, a, b) ->
    return x*y + a*b

double: calc <- {}, 2, ?, 2

Where ? could mean "that's still to be defined".

@zmthy
Copy link
Collaborator

zmthy commented Mar 23, 2010

Oh, so the curried double now takes one argument, which will be submitted as a? That's cool.
The defer example makes sense. I haven't worked out how you'd run a defer command on setTimeout yet. This way we can write
defer setTimeout ?, 2000
puts "Two seconds have passed!"

@weepy
Copy link
Author

weepy commented Mar 23, 2010

I always thought it made more sense to use the defer keyword itself to specify this:

setTimeout defer, 2000
puts "Two seconds have passed!"

@jashkenas
Copy link
Owner

My take on this is that it's still a little too obscure (rarely-used option to a rarely-used feature), and also that its use-case is mistaken -- if you're destructuring an array and only want one of the values, then:

[?, ?, x]: array

Isn't the way to write it. You want:

x: array[2]

Destructing assignment is more for when you want to pull multiple values out, and in that case, if you're writing a complex pattern match, it's usually instructive to name the variables, even if you're not using them. Of course, you can always use a throwaway variable if you really don't want to name the value, but like named functions by default, having pattern matches be named by default is a nice convention, I think, and makes for code that's easier to read.

@matehat
Copy link
Contributor

matehat commented Mar 24, 2010

jashkenas: What do you think about the other use cases pointed out?

@jashkenas
Copy link
Owner

Defer is still speculative, but I do think that placeholders are quite helpful for function binding / currying. You don't always want to curry from left-to-right or right-to-left. But in those rare cases, you can always curry by hand, by writing your own function, so it's still by no means essential.

This issue was closed.
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

5 participants