moonscript
moonscript copied to clipboard
[Feature Request] MoonScript VarArg Naming
In some of my code, I find myself using the extra arguments in a packed (named) table. For example:
stringify = (obj, ...) ->
opt = {...}
-- Do stuff with opt
In other programming languages, one is allowed to give a name to the variable arguments. In MoonScript, I would LOVE to be able to do it like so (and I think it still looks nice):
stringify = (obj, ...opt) ->
-- Do stuff with opt
Which may (or may not) translate to:
stringify = (obj, ...)
opt = {...}
-- Do stuff with opt
This could make some code snippets smaller in the long run. And I believe this would be a valuable addition to the MoonScript language (in terms of readability and succinctness).
+1 to this. Seems like a fairly small thing, but it would look pretty nice. Parallels with coffeescript as well.
+1
+1
@leafo Do you have any input on this?
:+1:
Some thoughts:
-
It should use
table.pack(...)
rather than{...}
so that one gets a chance to use afor i=1, opt.n
loop so as to both keep track of the indices and handlenil
arguments gracefully. Withfor x in *opt
you can do the latter but not the former. -
Wouldn't it be a good idea to use the Python-like
*
operator:foo = (bar, *opt) ->
MoonScript already uses
*foo
to mean "unpack and iteratefoo
" and extending it to in a signature mean "pack remaining/var args intofoo
" kind of makes sense, while...foo
is two more chars to type/fit into the line. -
I pretty much always want the options to be either a map table or the value of some default key, so I do this:
foo = (required, opt={}) -> unless 'table' == type opt opt = { default: opt } -- Do something with opt
making the first two of the following equivalent, but also allowing the third:
foo 'bar', 'baz' foo 'bar', {default: 'baz'} foo 'bar', {default: 'baz', other: 'quux'}
I'm so addicted to this pattern that I wish there was a syntax for it, like
foo = (req, *opt:default) -> -- do stuff
which would compile into something like this
local foo do foo = function (req, opt) if nil == opt then opt = {} elseif 'table' ~= type(opt) then opt = { default = opt } end -- do stuff end end