lua-users home
lua-l archive

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


2015-05-03 18:14 GMT+02:00 Sam Putman <atmanistan@gmail.com>:
>
>
> On Sun, May 3, 2015 at 8:32 AM, Nagaev Boris <bnagaev@gmail.com> wrote:
>>
>> On Sun, May 3, 2015 at 3:27 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>> > I can't guess what you want.
>>
>> I guess Sam wants strings to be like tables: each instance has
>> individual metatable.
>
>
> Precisely, but not if we have to pay for it.

The same effect is nearly free, see below.

> The string pointer has a metatable, so we can ("foo"):sub(1,1) and the like.
>
> It would be nice to be able to add to the metatable chain, so that values
> which
> point to strings can have arbitrary methods, not only one set of privileged
> string functions.

You can put a function in the "string" table and it will immediately
be callable with object-oriented syntax on a string object just like
str:format etc. Since the first argument is provided, that function
can itself pick out a submethod from a table of functions.