[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Why coroutine can't be indexed?
- From: Duncan Cross <duncan.cross@...>
- Date: Sat, 9 Jul 2011 15:08:53 +0100
On Sat, Jul 9, 2011 at 3:01 PM, HyperHacker <hyperhacker@gmail.com> wrote:
> On Sat, Jul 9, 2011 at 01:55, Dirk Laurie <dpl@sun.ac.za> wrote:
>> On Sat, Jul 09, 2011 at 04:13:06AM +0200, HyperHacker wrote:
>>> Just curious why, at least in Lua 5.1, one has to write:
>>> coroutine.status(co)
>>> instead of:
>>> co.status
>>> and similar? The latter seems a lot more Lua-like.
>>
>> Function call without parentheses? No-no. But I can see a case
>> for co:status(), etc. You can DIY with:
>>> debug.setmetatable(co,{__index=coroutine})
>> All coroutines then support object-oriented syntax sugar.
>>
>> Dirk
>>
>>
>
> Well, I was thinking status would be a field instead.
A coroutine does not have fields, only tables have fields. It's
possible to do metatable trickery to make non-tables appear to act
like tables, with hidden getter/setter methods or a weak table, but
the standard Lua libraries tend to keep things simpler than that.
-Duncan