moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

Explicit local variable declaration with :=

Open phoenixenero opened this issue 9 years ago • 7 comments

I'm a bit paranoid when declaring local variables in Lua/Moonscript; I like to make it explicit that the variable is local to the current scope.

Since I don't see the traditional Lua local variable declaration:

local foo = "bar"

anywhere in the Moonscript example code (or in Lapis), I assume that above syntax is not possible. So I usually just use

local foo
foo = "bar"

in my Moonscript code.

I propose the Go-like := variable declaration as syntactic sugar for explicit local variable declaration.

Examples:

foo := "bar"
baz := {}
baz.qux = "Hello"
y := type: "dog", legs: 4, tails: 1

say_hello := () ->
    foo := "World"
    print baz.qux .. " " .. foo

say_hello!

Should compile to:

local foo = "bar"
local baz = {}
baz.qux = "Hello"
local y = {type = "dog", legs = 4, tails = 1}

local say_hello
say_hello = function()
    local foo = "World"
    return print(baz.qux .. " " .. foo)
end

say_hello()

Reasons:

  • Concise syntax.
  • Current Moonscript code is still compatible with the new syntax.
  • Familiar to Go programmers.
  • Explicitly declaring local variables means you don't have to worry about variables accidentally leaking into the outer scopes.

Other notes:

The problem of implicit scoping is also documented in CoffeeScript (which is what Moonscript is based on.) See The Problem of Implicit Scoping in CoffeeScript (which actually gives the same solution as mine :D), CoffeeScript's Scoping is Madness.

phoenixenero avatar Jun 11 '16 12:06 phoenixenero

Variables are already done locally?

x = 5

compiles to:

local x = 5

Unless you want to reuse a variable name and define it in a local scope (which I personally really think is bad programming in general) it'd be safer to explicitly name it local using the keyword.

RyanSquared avatar Jun 12 '16 13:06 RyanSquared

The difference is that assigning new variables with := is more readable than using the generic assignment operator. Personally, I would really prefer this syntax, it's easier to read.

kameko avatar Jun 12 '16 22:06 kameko

@ChickenNuggers

Unless you want to reuse a variable name and define it in a local scope (which I personally really think is bad programming in general) it'd be safer to explicitly name it local using the keyword.

That situation does happen accidentally; see The Problem with Implicit Scoping in CoffeeScript which has similar rules regarding implicit variable declarations.

Making it explicit makes it less prone to accidentally assigning an existing variable with the same name.

phoenixenero avatar Jun 13 '16 10:06 phoenixenero

Personally I don't useusing feature in the code I write, so I wonder if it's a failed feature. In retrospect i think that features for safety shouldn't be optional otherwise you'll never use them. I'm every hesitant new operators though.

The using syntax, if you haven't seen it: http://moonscript.org/reference/#the-language/the-using-clause-controlling-destructive-assignment

One advantage of the using syntax is that it documents your intent at the top of your function, listing the closures you intend to use.

leafo avatar Jun 13 '16 16:06 leafo

I'm not really interested in adding a new operator, but I think allowing the syntax local x = "hello" could work. I originally made this invalid to prevent people from coming to lua from using local when it wasn't necessary.

leafo avatar Sep 25 '16 20:09 leafo

@leafo allowing 'local' with assignments should do it. explicit is better than implicit -- the zen of python

buckle2000 avatar Dec 09 '16 11:12 buckle2000

I also miss local var = val.

The issue tends to arise with reusable snippets of code which I include in scripts when using libraries is impractical, typically when declaring functions in a do block with preferably local helper variables and helper functions and the "public" function at the bottom.

@leafo I use the using feature a lot precisely because of this issue. In fact I wouldn't mind being able to use it on blocks too as an alternative to explicit local assignment.

bpj avatar Jan 24 '25 14:01 bpj