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:

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


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)

is equivalent to

string.split = function( self, delimiter )

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.:


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


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.





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:


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

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*

-- Lorenzo