object method call vs module call?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

object method call vs module call?

John Gabriele
Hi lua-l,

I understand that:

    > s = 'hello there hi'
    > -- this
    > s:find('there')
    7       11
    > -- is the same as
    > s.find(s, 'there')
    7       11

but what exactly is going on with:

    > -- this
    > s.find(s, 'there')
    7       11
    > -- vs
    > string.find(s, 'there')
    7       11

?

Thanks,
-- John

Reply | Threaded
Open this post in threaded view
|

Re: object method call vs module call?

Ryan
Since s is a string, its metatable is the string metatable, so it inherits all methods from string.  So s.find is the same method as string.find:
> s='hello there hi'
> s.find
function: 0x4225d0
> string.find
function: 0x4225d0

So you can execute either s.find or string.find and they give the same result.  This is most useful when using the object syntax you described, since s:find('there') can be used instead of s.find(s,'there') or string.find(s,'there').

On Fri, Jun 16, 2017 at 3:36 PM, John Gabriele <[hidden email]> wrote:
Hi lua-l,

I understand that:

    > s = 'hello there hi'
    > -- this
    > s:find('there')
    7       11
    > -- is the same as
    > s.find(s, 'there')
    7       11

but what exactly is going on with:

    > -- this
    > s.find(s, 'there')
    7       11
    > -- vs
    > string.find(s, 'there')
    7       11

?

Thanks,
-- John


Reply | Threaded
Open this post in threaded view
|

Re: object method call vs module call?

Charles Heywood
In reply to this post by John Gabriele
The way that `s:find()` works is that `s` has an index to `string`; you can compare `string.find` to `s.find` and find that they *should* be equal (if not, they should implement the exact same functionality at least). When you call `object:method()`, you are doing the equivalent of `object.method(object)`, so `s.find(s, 'there')` is equivalent to `s:find('there')`.

On Fri, Jun 16, 2017 at 2:37 PM John Gabriele <[hidden email]> wrote:
Hi lua-l,

I understand that:

    > s = 'hello there hi'
    > -- this
    > s:find('there')
    7       11
    > -- is the same as
    > s.find(s, 'there')
    7       11

but what exactly is going on with:

    > -- this
    > s.find(s, 'there')
    7       11
    > -- vs
    > string.find(s, 'there')
    7       11

?

Thanks,
-- John

--
--
Ryan <[hidden email]>
Software Developer / System Administrator
Reply | Threaded
Open this post in threaded view
|

Re: object method call vs module call?

Jonathan Goble
In reply to this post by John Gabriele
On Fri, Jun 16, 2017 at 3:37 PM John Gabriele <[hidden email]> wrote:
Hi lua-l,

I understand that:

    > s = 'hello there hi'
    > -- this
    > s:find('there')
    7       11
    > -- is the same as
    > s.find(s, 'there')
    7       11

but what exactly is going on with:

    > -- this
    > s.find(s, 'there')
    7       11
    > -- vs
    > string.find(s, 'there')
    7       11

?

Thanks,
-- John

Since Lua 5.1, strings have a metatable with an __index field referencing the `string` table. Since strings cannot be directly indexed, any attempt to index a string (e.g. `s.find(s, 'there')`) falls back to the __index metamethod, which tells Lua to repeat the indexing in the `string` table. The last example (`string.find(s, 'there')`) simply bypasses that metatable completely and indexes the `string` table directly. Either way, it's the same function: `s.find == string.find`.
Reply | Threaded
Open this post in threaded view
|

Re: object method call vs module call?

John Gabriele
On Fri, Jun 16, 2017, at 04:05 PM, Jonathan Goble wrote:
On Fri, Jun 16, 2017 at 3:37 PM John Gabriele <[hidden email]> wrote:
Hi lua-l,

I understand that:

    > s = 'hello there hi'
    > -- this
    > s:find('there')
    7       11
    > -- is the same as
    > s.find(s, 'there')
    7       11

but what exactly is going on with:

    > -- this
    > s.find(s, 'there')
    7       11
    > -- vs
    > string.find(s, 'there')
    7       11

?

Thanks,
-- John

Since Lua 5.1, strings have a metatable with an __index field referencing the `string` table. Since strings cannot be directly indexed, any attempt to index a string (e.g. `s.find(s, 'there')`) falls back to the __index metamethod, which tells Lua to repeat the indexing in the `string` table. The last example (`string.find(s, 'there')`) simply bypasses that metatable completely and indexes the `string` table directly. Either way, it's the same function: `s.find == string.find`.


Thanks, Jon. What does "indexed" mean in that context?

Reply | Threaded
Open this post in threaded view
|

Re: object method call vs module call?

John Gabriele
In reply to this post by Ryan
Thanks, all!


On Fri, Jun 16, 2017, at 03:40 PM, Ryan wrote:
Since s is a string, its metatable is the string metatable, so it inherits all methods from string.  So s.find is the same method as string.find:
> s='hello there hi'
> s.find
function: 0x4225d0
> string.find
function: 0x4225d0

So you can execute either s.find or string.find and they give the same result.  This is most useful when using the object syntax you described, since s:find('there') can be used instead of s.find(s,'there') or string.find(s,'there').

On Fri, Jun 16, 2017 at 3:36 PM, John Gabriele <[hidden email]> wrote:
Hi lua-l,

I understand that:

    > s = 'hello there hi'
    > -- this
    > s:find('there')
    7       11
    > -- is the same as
    > s.find(s, 'there')
    7       11

but what exactly is going on with:

    > -- this
    > s.find(s, 'there')
    7       11
    > -- vs
    > string.find(s, 'there')
    7       11

?

Thanks,
-- John


Reply | Threaded
Open this post in threaded view
|

Re: object method call vs module call?

Jonathan Goble
In reply to this post by John Gabriele
On Fri, Jun 16, 2017 at 5:02 PM John Gabriele <[hidden email]> wrote:
On Fri, Jun 16, 2017, at 04:05 PM, Jonathan Goble wrote:
On Fri, Jun 16, 2017 at 3:37 PM John Gabriele <[hidden email]> wrote:
Hi lua-l,

I understand that:

    > s = 'hello there hi'
    > -- this
    > s:find('there')
    7       11
    > -- is the same as
    > s.find(s, 'there')
    7       11

but what exactly is going on with:

    > -- this
    > s.find(s, 'there')
    7       11
    > -- vs
    > string.find(s, 'there')
    7       11

?

Thanks,
-- John

Since Lua 5.1, strings have a metatable with an __index field referencing the `string` table. Since strings cannot be directly indexed, any attempt to index a string (e.g. `s.find(s, 'there')`) falls back to the __index metamethod, which tells Lua to repeat the indexing in the `string` table. The last example (`string.find(s, 'there')`) simply bypasses that metatable completely and indexes the `string` table directly. Either way, it's the same function: `s.find == string.find`.

Thanks, Jon. What does "indexed" mean in that context?

By "index", I mean simply look up a key in an object (this is a common Lua term). string.find means "index the 'string' table with the key 'find'". Only tables have a built-in notion of indexing. Attempting to index anything else fails instantly, unless it has a metatable with an __index field (which strings by default do).
Reply | Threaded
Open this post in threaded view
|

Re: object method call vs module call?

John Gabriele
On Fri, Jun 16, 2017, at 05:19 PM, Jonathan Goble wrote:
On Fri, Jun 16, 2017 at 5:02 PM John Gabriele <[hidden email]> wrote:
On Fri, Jun 16, 2017, at 04:05 PM, Jonathan Goble wrote:
On Fri, Jun 16, 2017 at 3:37 PM John Gabriele <[hidden email]> wrote:
Hi lua-l,

I understand that:

    > s = 'hello there hi'
    > -- this
    > s:find('there')
    7       11
    > -- is the same as
    > s.find(s, 'there')
    7       11

but what exactly is going on with:

    > -- this
    > s.find(s, 'there')
    7       11
    > -- vs
    > string.find(s, 'there')
    7       11

?

Thanks,
-- John

Since Lua 5.1, strings have a metatable with an __index field referencing the `string` table. Since strings cannot be directly indexed, any attempt to index a string (e.g. `s.find(s, 'there')`) falls back to the __index metamethod, which tells Lua to repeat the indexing in the `string` table. The last example (`string.find(s, 'there')`) simply bypasses that metatable completely and indexes the `string` table directly. Either way, it's the same function: `s.find == string.find`.

Thanks, Jon. What does "indexed" mean in that context?

By "index", I mean simply look up a key in an object (this is a common Lua term). string.find means "index the 'string' table with the key 'find'". Only tables have a built-in notion of indexing. Attempting to index anything else fails instantly, unless it has a metatable with an __index field (which strings by default do).

Gotcha. Thanks!