moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

This looks too great to exist...

Open handicraftsman opened this issue 8 years ago • 9 comments

load = (name) ->
  with io.open(name)
    while txt = \read
      print txt

(i meant that you should really make with statement interact with given object. This will not change basic syntax, but will allow with items["stick"]-like statements)

handicraftsman avatar Jan 30 '17 20:01 handicraftsman

Can you un-clickbait the title? Also, I don't believe MoonScript supports assignment with while loops.

RyanSquared avatar Jan 31 '17 01:01 RyanSquared

MoonScript really support it, e.g.

n = 0
foo = while n < 10
  n += 1
  n

foo will be a table have 1...10 as values.

starwing avatar Jan 31 '17 05:01 starwing

It's not documented in the reference, then. I checked under the section for while - http://moonscript.org/reference/#the-language/while-loop - and couldn't see anything.

RyanSquared avatar Jan 31 '17 06:01 RyanSquared

I believe you three are not talking about the same thing.

@RyanSquared said "this is wrong":

while txt = \read
      print txt

buckle2000 avatar Jan 31 '17 11:01 buckle2000

You can already put any expression in a with statement. It's not possible to put assignment in a while loop.

leafo avatar Jan 31 '17 22:01 leafo

Alternate syntax, that does work (in theory):

load = (name)->
  with file = io.open name
    print line for line in file\lines!

Please note that if you want this to be like Pythonic with so that it's garbage collected when the with line ends, that's not going to happen because with either syntax, the with statement returns the file, meaning it's now in the scope of the function calling load().

RyanSquared avatar Jan 31 '17 23:01 RyanSquared

@leafo, it is kinda strange.

handicraftsman avatar Feb 03 '17 09:02 handicraftsman

@handicraftsman I still don't know what do you mean...

buckle2000 avatar Feb 03 '17 12:02 buckle2000

Would it be possible to compile while a = b to something like this?

local a, _fn
_fn = function ()
  return a = b
end
while _fn() do
  ...
end

vendethiel avatar Dec 14 '17 10:12 vendethiel