[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: try..catch for Lua 5.2?
- From: James Rhodes <jrhodes@...>
- Date: Thu, 16 Sep 2010 19:20:09 +1000
I intend to modify Lua to support both type evaluation and advanced
try-catch statements (like those above which take a type parameter).
Essentially it's the addition of a __type metamethod, which can return
either a string to identify the class name, or a table with two
strings, one for the name of the derived (current) class, and one for
the name of the parent class. If the derived class string doesn't
match what is searched for, it looks up the object defined by the
parent class string, and calls __type on it. It repeats the process
until it can determine whether the type matches or not.
There is also the introduction of the 'is' operator, which compares
the results of two __type metamethods. A suggestion is to add a
__type metamethod to the class constructor (after you define it) and
to also make the class constructor add the same __type metamethod to
the instance, so that you can do:
myobj = MyClass() -- The MyClass function has a __type metamethod
which returns "MyClass".
if (myobj is MyClass) then
-- do stuff
end
The __type evaluations for string and numbers should be 'string' and
'number' respectively (although this is built-in to the type
evaluation function since strings and numbers can't have metamethods).
Tables without __type also evaluate to 'table'.
With the addition of the __type metamethod, the catch clause is then
able to compare the type specified in it, with the __type metamethod,
so that catch clauses can be written like:
try
-- stuff
catch (MyClass e)
-- stuff
catch (OtherClass e)
-- stuff
catch (e)
-- default catch clause
end
There's more detail at
http://code.google.com/p/roket3d/wiki/PlannedUpgrades, but if it's a
highly wanted feature for inclusion in 5.2, I'll submit a patch which
makes the above changes when I've implemented them in my copy of Lua
(since I need them for my own project anyway).
Regards, James.
On Tue, Sep 14, 2010 at 4:10 AM, Duncan Cross <duncan.cross@gmail.com> wrote:
> On Mon, Sep 13, 2010 at 6:24 PM, Duncan Cross <duncan.cross@gmail.com> wrote:
>> How about a syntax something like this:
>
> Or, another variation - instead of a comma followed by a function
> name, an optional assignment expression that can refer to the left
> hand side:
>
> try
> -- ... [1]
> catch e = MyException(e) do
> -- ... [2]
> catch e = MyOtherException(e) do
> -- ... [3]
> catch e do
> -- ... [4]
> end
>
> ...which would be sugar for:
>
> try
> -- ... [1]
> catch _ERROR do
> do
> local e = MyException(_ERROR)
> if e then
> -- ... [2]
> _ERROR = nil
> end
> end
> if _ERROR then
> local e = MyOtherException(_ERROR)
> if e then
> -- ... [3]
> _ERROR = nil
> end
> end
> if _ERROR then
> local e = _ERROR
> -- ... [4]
> end
> end
>
> (Except, of course, _ERROR wouldn't really exist as a named variable.)
>
> -Duncan
>
>