Moonscript uses \ to call methods so can someone explain to me why the code below does not work:
> file = io\open("mix.exs", "rb")
[string "tmp"]:1: calling 'open' on bad self (string expected, got table)
but when you call it to read the file it does ?
> file\read!
"Code.ensure_loaded?(Hex) and Hex.start
to call member methods. as in
a\b c, ...
translates toa.b(a,c,...)
.this doesn't work here because
io.open
is a static function (io.open(what,how)
), not a member (io.open(self,what,how)
).you couldn't call
io:open
in Lua either. the only place whereio
functions allow for being called as members is when you want to read/write stdio.because now it's a member method of the file. you're actually still using
io.read
there, but the file object hasio
as a metatable index, therefore allowing you to access the same function viafile.read
, and sincefile\read!
translates tofile.read(file)
it's the same thing.so essentially the answer boils down to "because
io:open
doesn't work in Lua".