4 ms·
fixing the gaps in Lua. Moonscript's [...] default local Roberto Ierusalimschy, the creator of Lua, believes that "Local by default is wrong." [1]. I don't ne
by bakery2k 9y ago
fixing the gaps in Lua. Moonscript's [...] default local
Roberto Ierusalimschy, the creator of Lua, believes that "Local by default is wrong." [1].
I don't necessarily agree that local-by-default is inherently wrong, but every implementation of local-by-default that I've seen has had flaws.
It looks like MoonScript is no different - its scoping rules seem to suffer from the same "madness" as CoffeeScript [2].
[1] http://lua-users.org/wiki/LocalByDefault http://lua-users.org/wiki/LocalByDefault
[2] https://donatstudios.com/CoffeeScript-Madness https://donatstudios.com/CoffeeScript-Madness
- allover 9y agoEugh, I know you aren't defending that CoffeeScript article, but it hugely exaggerates the 'problem' by not acknowledging module scope (see the comments).
- cosarara97 9y agoYou can use 'local' to have sane scoping (shadowing) in moonscript though: -- will print: -- 15 -- 0 -- 15 -- 10 y = 0 goodfun = (x) -> local y y = 10 y + x badfun = (x) -> y = 10 y + x print(goodfun(5)) print(y) print(badfun(5)) print(y)
- Pxtl 9y agoHonestly, it seems like implicit variable declaration is a bad idea. Some minimal keyword to indicate "this is a new variable in this scope" seems necessary for sane scoping. It nicely prevents a class of bugs of "oops I made a typo in that assignment and created a new variable" gets caught.
- abecedarius 9y agoIndeed. Is "let x = y+y" so bad? I wonder how much of the resistance owes to longer words like "local"/"define", or ugly abbrevs like "mut"/"var".
- Pxtl 9y agoSo what would you use for declaring a variable that won't be assigned to until lower scope? Like a variable that will be assigned inside a try/catch block but needs to outlive it so it's declared before the try? Just let x ? That looks weird. Or we're back to "var". Or throwing another keyword at x? let x exist Also weird. I've often dreamed of a nice lua-like math with syntax aimed at being familiar to a first year college math student, and "let" is an obvious keyword for variable declaration, but unless you go pure functional you need to be able to pre-declare variables.
- abecedarius 9y agoI'd make try/catch an expression, not a statement, like in Lisp -- it'd still be "let x = try {...}...". I agree that 'var' for mutable bindings is a reasonable design. In E it's 'def' for immutable and 'var' for mutable, for instance; and you can also write just 'def x' and then 'bind x := 42' further down (with single-assignment semantics). But though that's nice to have, you can do all right without it; if there was just a single form of definition, with lexical scope, I'd still prefer it to Lua or Python's designs -- clearer and less error-prone and still concise.
- Pxtl 9y agoI'd use "set" instead of "bind" and be consistent that assignment leverages the vanilla equality operator (since we're already talking about that for "let") but yeah, it sounds like you've been thinking about the same stuff I have. It sounds like you've been pondering the same problems I have. In my case it's because I've been working on a rules-engine for scientists and currently we're using SQL for the rule language and some of the syntactic warts of SQL are tripping us up. I keep meaning to learn enough Racket Scheme to experiment with implementing such a language in macros.
- abecedarius 9y agoFWIW, Common Lisp's macro system is relatively simple. I've been meaning to check out http://beautifulracket.com/ http://beautifulracket.com/ sometime to learn about the Racket way. Maybe Datalog could make a cleaner base for your project? Just a random thought; bound to be lots more considerations. Yeah, I have a project right now too where these questions come up -- https://github.com/darius/squeam https://github.com/darius/squeam -- though it's not really ready for anyone.