On 10/19/2012 11:43 AM, Manuel Kasser wrote:
Hi,
I've got a question not especially concerning awesome, but I'm sure some
of you will know it: What's the ":"-operator for when I use it on an
object (e.g. string:name() if that is a valid example).
Searching for it with my standard search engine didn't help because I
get a lot of lua-tutorials and wiki-pages, but those where the ":" is
used to separate examples of code from the introductional text etc.,
which doesn't really help and I had no luck in finding a useful wiki
page, where the operator is explained, just some, where it is declared
as a special operator. Could someone help me?
Thanks in advance,
Manuel
I guess the link Clément kindly provided might be a bit hard to read
through, so here's a simpler explanation.
":" operator is an alias to invoke a function on an object with "." and
put the object itself as a first argument. Consider the example:
o = { }
function o.foo(this, somearg, otherarg)
-- code here
end
-- The following to invocations are equivalent:
o.foo(o, "quick brown fox", 42)
o:foo("quick brown fox", 42)
So as you can see a colon operator is used to emulate object-oriented
behavior like those OOP languages have.
Note that you can't just write a function somewhere (for example, for
splitting strings), and call it on your strings that way. In order for
this to work the object itself must have the function you call as one of
its fields.
Kind regards,
--
Alexander Yakushev
"Being really good at C++ is like being really good at using rocks to
sharpen sticks."
-- Thant Tessman
--
To unsubscribe, send mail to [email protected].