Where is the documentation about :upper, :sub, :gsub, :replace, and so on?
And what is it called/terminology?
From what I know this is from Lua itself and not something defined in the FrameXML, since they also work in the live demo.
I see it used everywhere a bit, but nothing about where it actually comes from.
Then the following code should also work, right? I can't test this out now, since the EU servers are atm down for patch 4.0.3.
string.sub(s, 1, 2)
strsub(s, 1, 2)
sub(s, 1, 2)
And vice versa?
s:string.sub(1, 2) >> live demo: error
s:strsub(1, 2)
s:sub(1, 2)
Edit @ Farmbuyer: sorry I still actually got no idea what object-oriented meant (maybe now), and overread that part
Besides, the reference manual is hard to read for noobs ><
I'll expand on this a bit. Say you write the expression on the left:
s:byte(i)
The first thing Lua does (conceptually anyhow, the engine itself is smarter than all these steps I'm writing here) is to replace the "colon/method call" sugar with normal field access:
s.byte(s,i)
Next it replaces the "field access" sugar with actual key lookup:
s["byte"](s,i)
Now it tries to index 's' to find a key "byte"... except it can't, because strings don't have keys. If 's' were a table, it would actually look inside it, but since it's a string Lua skips the nonsensical lookup. The following steps are the same though.
Next it checks whether 's' has a metatable. (If the standard string library is loaded, then all strings are set to use the same metatable.)
Next it checks whether the metatable has an __index key. It does.
Next it checks whether __index refers to a function or to a table. In this case, it's a table, so the key lookup is repeated in that table. For strings, __index is set to point to the table called 'string' that contains all the library functions. So the final form is
string["byte"](s,i)
And again, that's more of a mental walkthrough, the Lua engine isn't doing all these teensy steps each time somebody writes str:lower().
Besides, the reference manual is hard to read for noobs ><
It's definitely meant as a dry reference, not a friendly tutorial. If you have some spare change to drop, I highly recommend buying the 2nd edition of Programming in Lua. It's only available in print, not online, but the additional material and rewritten sections are a big improvement over the free online first edition.
And what is it called/terminology?
From what I know this is from Lua itself and not something defined in the FrameXML, since they also work in the live demo.
I see it used everywhere a bit, but nothing about where it actually comes from.
I first tried looking into these resources:
http://lua-users.org/wiki/ColonForMethodCall
I didn't know what ColonForMethodCall was, but then this means it's also "syntactical sugar" for the following code?!
Then the following code should also work, right? I can't test this out now, since the EU servers are atm down for patch 4.0.3.
Besides, the reference manual is hard to read for noobs ><
and from earlier in the manual
That's the only substitution available for method calls:
s:byte(i) <-> string.byte(s,i)
Rearranging any of those identifiers into arbitrary order isn't legit. :-p
I'll expand on this a bit. Say you write the expression on the left:
The first thing Lua does (conceptually anyhow, the engine itself is smarter than all these steps I'm writing here) is to replace the "colon/method call" sugar with normal field access:
Next it replaces the "field access" sugar with actual key lookup:
Now it tries to index 's' to find a key "byte"... except it can't, because strings don't have keys. If 's' were a table, it would actually look inside it, but since it's a string Lua skips the nonsensical lookup. The following steps are the same though.
Next it checks whether 's' has a metatable. (If the standard string library is loaded, then all strings are set to use the same metatable.)
Next it checks whether the metatable has an __index key. It does.
Next it checks whether __index refers to a function or to a table. In this case, it's a table, so the key lookup is repeated in that table. For strings, __index is set to point to the table called 'string' that contains all the library functions. So the final form is
And again, that's more of a mental walkthrough, the Lua engine isn't doing all these teensy steps each time somebody writes str:lower().
It's definitely meant as a dry reference, not a friendly tutorial. If you have some spare change to drop, I highly recommend buying the 2nd edition of Programming in Lua. It's only available in print, not online, but the additional material and rewritten sections are a big improvement over the free online first edition.
What you typed there s:string.sub(1,2)
would mean in loose english
"call the method 'string' of 's' passing itself (s) as a first argument"
I followed most of Farmbuyers' "desugarfying", until the metatable and __index keys, where it got a bit too, ehh, complicated
I should really try to grab the 2nd edition of PIL in europe~
Lastly, thanks Dridzt and Farmbuyer for answering my questions (n_n)
I also find the reference manual hard to read and I've been a programmer for 10 years.