[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Custom type methods (and name)
- From: Ivan <ivan@...>
- Date: Fri, 19 Feb 2016 12:01:36 +0300
19.02.2016, 08:14, "Dirk Laurie" <firstname.lastname@example.org>:
> 2016-02-18 22:46 GMT+02:00 Ivan <email@example.com>:
>> I’m trying to create my own type (for complex numbers) and
>> use it within Lua scripts.
> This has been done by Luiz himself (Google: complex lua lhf).
Ok, but complex is not the only custom type I need.
>> It’s okay but the problem is that in some messages this type is
>> denoted as ‘userdata’ which looks ugly. I have found that if type’s
>> metatable has __name field, that it’s value is used as a type name.
>> Unfortunately this key is not used everywhere, e.g. pairs(complex)
>> still uses ‘userdata’ name. Is there a way I can make Lua to use
>> custom type name?
> Under Lua 5.3.2 with Luiz's complex, I get:
>> z=complex.new(0,1); pairs(z)
> stdin:1: bad argument #1 to 'pairs' (table expected, got complex number)
> So I can't reproduce your result.
> The manual says only:
> The entry __name is used by some error-reporting functions.
> The word "some" usually means "at least one but not all". I suspect
> that the vagueness is deliberate, and if you could provide a list of
> which error messages you would like to have included too, some (!)
> might get into Lua 5.3.3.
local cx = complex.new(0, 0)
local cx2 = complex.new(1, 1)
print(cx < cx2)
and the mesasge 'attempt to compare two userdata values'
It seems that I have to implement all of the metamethods (even those that are not supported by the domain) nad provide my own error messages.
And still I'm looking for a way to use both OOP style (my_array:length()) and array-style (my_array = 0)