lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Please, switch your mail client to plaint text.
HTML posting is considered bad netiquette in this list.

On 27/06/2011 18.37, Dave Collins wrote:
I’ve used this split (string to table) method I found here:
http://stackoverflow.com/questions/1426954/split-string-in-lua.

I’ve got it working, but I don’t understand it.


[snip]

First of all you must understand that ":" is Lua way to call a method for an object, but under the hood is just syntactic sugar.

The *definition*

function string:split(delimiter)
...
end

is equivalent to

string.split = function( self, delimiter )
...
end

i.e. you are assigning to the field named "split" of table "string" a new function whose first argument (automatically inserted by Lua under the hood) is called "self" (it is like "this" in C++ or Java - it references the object on which the method is called).



This is how I’m calling it:

*local*myDateString = "2011-06-21"

*local*myDateTbl = myDateString.split(myDateString,"-")


When defining a new method this way, it is customary to call it using OOP syntax, i.e.:

myDateString:split("-")

but even here, at the call site, ":" is syntactic sugar for

myDateString.split(myDateString,"-")

which means:

1. get the field "split" from table myDateString
2. call it passing it two arguments: myDateString and "_"

Regarding 1: since myDateString is a string and every string has by default the table "string" as metatable [1], if a field ("split" in this case) is requested, the field is looked-up in the metatable. In this case the previously defined function (string.split) is returned.

Regarding 2: since that function takes two parameter, self and delimiter, they are assigned the arguments from the call; therefore
self gets myDateString and delimiter gets "-" as values.


dump_table(myDateTbl)

1=2011

2=06

3=21

Two things I don’t understand:

1] When I call it, I reference it twice:
myDateString.split(myDateString,"-")Why? Or should I just do this:
string.split(myDateString,"-") Which works, so I guess the answer is yes.

both are correct, if you followed the explanation above; as I said, it is less verbose and more OOP to say:

myDateString:split("-")



2] The function takes one parameter (delimiter), but I must pass it two
(myDateString,"-"). If I only pass it the delimiter string like it seems
to want, it breaks. Why does it take two when it looks like it takes one?


This happens because you define the function using the OOP style, so the parameter self is hidden.

Lua doesn't have "classes" and so knows almost nothing about "methods", it only has syntactic facilities to help with OOP style.

A method is just a regular function whose first parameter is the object Even the name "self" is conventional. The Lua uses this name when defining a function with the OO style, but if you want to be explicit you could also use other names. E.g.:

string.split = function( obj, delimiter )
...
-- replace any "self" with "obj" and it will work the same
end



Are these both occurring because I’m not really writing a function, I’m
really modifying the string prototype?

Well, Lua has no prototypes, it has metatables. But in a way, yes, you are adding custom behaviour to the string type.

Note that by someone modifying or adding items to the default libraries (such as the "string" table) is considered bad practice. Expecially in large scale apps.



*Dave Collins*

Cheers
-- Lorenzo


[1] http://www.lua.org/manual/5.1/manual.html#2.8