1

How do I do this in Moonscript?

function a:do_something(b)
    print(b)
end

Nothing I tried would compile and I didn't see anything in their documentation.

Farzher
  • 13,934
  • 21
  • 69
  • 100

3 Answers3

2

In Lua what you wrote is syntactic sugar for the following:

a.do_something = function(self, b)
  print(b)
end

So you would do just that in MoonScript. (note the => as a shorthand for adding self to the front of the function's argument list)

a.do_something = (b) =>
  print b
leafo
  • 1,862
  • 14
  • 18
1

In MoonScript you'd do:

a.dosomething = (self, b) ->
  print b

The -> and => symbols are aliases of the function keyword.

a.dosomething = (b) =>
  print b

Using the => (Fat arrow) style as above, adds the scope, ie. self, to the arguments list automatically.

ocodo
  • 29,401
  • 18
  • 105
  • 117
huli
  • 101
  • 1
  • 6
0

what you're looking for is class.__base:

class C
  a: (x)=> print x

C.__base.b = (y)=> @a y*2

i=C!

i\b 5
--prints 10
nonchip
  • 1,084
  • 1
  • 18
  • 36