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.openis a static function (io.open(what,how)), not a member (io.open(self,what,how)).you couldn't call
io:openin Lua either. the only place whereiofunctions 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.readthere, but the file object hasioas 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:opendoesn't work in Lua".