moonscript
moonscript copied to clipboard
return *
I like the local * and export * features, and I find myself wishing for some way to return all variables that aren't explicitly local.
local x
x = 1
y = 2
z = 3
return *
..would compile to:
local x
x = 1
local y
y = 2
local z
z = 3
return {
y = y,
z = z,
}
Although, it might be nice to plop them in the returned table if they're never referenced.
local x
x = 1
return {
y = 2,
z = 3,
}
I'm not sure how using both local * and return * would work yet.
@vendethiel Curious why you don't like the idea. 😄
Another option would be to support multiple return keywords at the top-level of a file.
return foo = ->
return bar = ->
..which would compile to:
local foo
foo = function() end
local bar
bar = function() end
return {
foo = foo,
bar = bar,
}
This would work great with local * too.
I don't see the issue with how it currently is:
return {
hello: -> "hi"
}
Also you could do return :hello and define it earlier if that's your preference.
Sorry, I should have stated the annoyance in the OP.
The current options obviously work, but I would like to avoid two things:
-
Typing returned property names twice (declaration and return) when using
return :foo -
Unwanted indentation when using
return { }
It's not a huge issue, but I saw export * and thought return * may be interesting.
I think the ideal would be making export return the variable instead of making it global, but that's a breaking change, so no dice.
The next best thing is allowing multiple returns at the top-level, but that could be a footgun or just plain confusing to people.
Here's an edgy idea. Support table returns without indentation.
foo: ->
bar: ->
..would compile to:
return {
foo = function() end,
bar = function() end,
}
Note that the table would need to be the last declaration in the file.
Of course, this idea doesn't avoid the first annoyance.
@vendethiel Curious why you don't like the idea.
All these are a bit too action-at-a-distance-y for me, to be honest.
My main issue with the original suggestion is that it overloads the use of local to now be responsible for identifying things that can't be exported.
I think having the last line in your file be an explicit list of the things you want to export to be a better approach, since you won't be exporting things by accident when you create a new variables.
Also, as an aside, files have implicit return just like functions. You don't need to write return, your last line could be:
{ :my_function, :something_else, :VERSION }
Here's an edgy idea. Support table returns without indentation.
I don't think it's that edgy :). It's probably a good idea. CoffeeScript supports this syntax. I don't know off the top of my head how complex this change would be in terms of syntax conflicts.
@leafo Glad to hear you like that solution! I love that syntax. I even tried doing it once, only to realize it doesn't work.