jashkenas / coffeescript

Unfancy JavaScript
https://coffeescript.org/
MIT License
16.49k stars 1.99k forks source link

Request: Allow omit parameters unused #5165

Closed TrungRueta closed 5 years ago

TrungRueta commented 5 years ago

Feature Request

Dear team, Since Coffeescript get a lot of ideas and habit from Functional programming languages, i would like to order feature allow skip parameters unused before used ones. My english is not good for explains so i will write example.

a lot of time when we writing code , we have module call to callback function like this:

mod = (cbFunc) ->
    one = 1
    two = 2
    cbFunc one two

Then in other places we defined function callback for module, and we only want get data from second param:

cb = (one, two) -> console.log two
mod cb

Aa you can see, we only want use second param but we always must define first param. In haskell (if i not wrong ) they had syntax allow skip param before used ones liek this:

cb = (, two) -> console.log two

Look like it is not much change, but if working 1000+ line in project then always typing define name of variable for parameters we not use in function is tired. With me, when not used i awalys type params under name "undescore":

cb = (_, two) -> console.log two

but it only clear with 2 params, if 3 and want use last, it must be this

cb = (_, __, three) -> console.log three

and it start ugrly, if Coffescript can handle this case and allow omit typing param not use just like spread array: [,,three] feature then it will be wonderfull, reduce typing a lot

when render to js i think coffeescript just need compile omitted param into random variable name or unique like ___thisisomited1 ___thisisomited2 etc.

Hope everyone discuss about this . Thank you!

Inve1951 commented 5 years ago

While I found myself trying this several times in the past and being in favor of the syntax, I'm afraid we won't get this feature as ATM there's support for an optional comma, making this a breaking change.

f = (,x) -> x

currently compiles to

var f;

f = function(x) {
  return x;
};
TrungRueta commented 5 years ago

can you explains more detail about this. does developers still use (,paramN) way in code since v1 ? because i no see this this our current document website.

An another way i can think for now is call function use spread . Follow my example with function mod i can write code like this:

cb = (params...) ->
    [,,param3] = params
    ...

but it will put extra step :(

Asc2011 commented 5 years ago
# your callback-fn
cb = (one, two, three, four) ->
  console.log one,two,three, four

# helper returns a fn
_ = (pos) ->
  (cb, param) ->
    cbArgs = new Array cb.length
    cbArgs[pos] = param
    cb.call ...cbArgs

# define what you need
_2 = _ 2
_3 = _ 3
_4 = _ 4

# use your helper(s)
_2 cb, 'test2'
_3 cb, 'test3'
_4 cb, 'test4'

# without define
(_ 3) cb, 'test3'
carlsmith commented 5 years ago

@TrungRueta - In the (very rare) case that you need to throw away more than two preceding arguments, but keep the last one, you shouldn't use positional arguments. This...

cb = (_, __, ___, four) -> console.log four

...could be done this way...

cb = -> console.log arguments[3]

...or a few other ways, depending on the circumstances. Not least, just avoid writing/using code that defines callback signatures with long lists of positional arguments that may all go unused, except for the last one ;)

The problem seems unlikely to ever come up much, and there are already a few ways around it if it every does. Adding sugar here wouldn't make sense.

GeoffreyBooth commented 5 years ago

I think this is something that we should add support for if JavaScript does, but not otherwise. There’s nothing new about this use case; skipping function arguments is something that people could have (did?) want throughout CoffeeScript’s history, yet it’s never been a pressing enough need to add it thus far. Especially if adding it causes a breaking change, that’s a strong argument for leaving this out.