moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

Overwriting _

Open buckle2000 opened this issue 8 years ago • 6 comments

{a,b,_} = {1,2,3}
1
2

compiles to

local a, b, _
do
  local _obj_0 = {
    1,
    2,
    3
  }
  a, b, _ = _obj_0[1], _obj_0[2], _obj_0[3]
end
_ = 1
return 2

_: f*ck, I got overwritten!

buckle2000 avatar Dec 09 '16 07:12 buckle2000

Is this because you've like to actually use the _ variable? MoonScript typically treats it as a placeholder, but it's an unwritten rule that you should avoid reading from it.

leafo avatar Dec 09 '16 18:12 leafo

I prefer to use _ as a placeholder.

I just want to say that if _ can be overwritten, moonscript might not have a safe scoping system.

buckle2000 avatar Dec 10 '16 00:12 buckle2000

Some people prefer to use _ for utilities like this one: https://github.com/Yonaba/Moses

buckle2000 avatar Dec 18 '16 06:12 buckle2000

I can think of 3 solutions.

  1. Change _ into a long name like _moonscript_<checksum>
  2. Ban the usage of writing a single value on one line
  3. Ignore these lines (like comments)

buckle2000 avatar Jan 08 '17 02:01 buckle2000

I think MoonScript should use its own variable name as a placeholder instead of _ to get around this potentially causing problems.

I would tend to disagree with the need for the change, but if a newbie is coming along who doesn't understand the tradition to use _ only as a placeholder, this makes it even harder for them to get into programming, which is hard enough as it is. :P

TangentFoxy avatar Feb 26 '17 02:02 TangentFoxy

If that's of anything interest, a similar discussion was had in a CoffeeScript fork with a feature similar to moon's with:

https://github.com/satyr/coco/issues/181 https://github.com/satyr/coco/issues/90

vendethiel avatar Dec 13 '17 23:12 vendethiel