[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: More type checking in Ravi
- From: Dibyendu Majumdar <mobile@...>
- Date: Sat, 24 Mar 2018 01:33:35 +0000
On 15 October 2017 at 11:06, Dibyendu Majumdar <mobile@majumdar.org.uk> wrote:
> We can also emulate this in Lua:
>
> mt = { __name = 'MyType' }
> -- register metatable
> debug.getregistry().MyType = mt
>
> a = {} -- some table
> setmetatable(a, mt)
>
> Now we can write:
>
> function x(a: MyType) print(a) end
>
> -- Call x with a
> x(a) -- Okay
>
> -- Call x with random table
> x({1,2}) -- Fails as follows:
>
> stdin:1: type mismatch: expected MyType
> stack traceback:
> stdin:1: in function 'x'
> (...tail calls...)
> [C]: in ?
>
I previously reported that I have added support for annotating
variables with user defined type names - as defined by __name
attribute in the metatable. One limitation of the limitation until now
was that the type name had to be a valid Lua name, so that the name
could not have a period ('.') embedded in it. I have committed an
update that allows this. So one can now write:
> mt = { __name = 'ravi.MyType' }
> debug.getregistry()['ravi.MyType'] = mt
> a = {}
> setmetatable(a, mt)
ravi.MyType: 0000017110BE2DD0
> function x(a: ravi.MyType) print(a) end
> x(a)
ravi.MyType: 0000017110BE2DD0
> x({1,2})
stdin:-842150451: type mismatch: expected ravi.MyType
stack traceback:
stdin: in function 'x'
(...tail calls...)
[C]: in ?
This feature is still not very well tested; also there is some
outstanding work to do with asserting types when accessed via
upvalues.
Thanks and Regards
Dibyendu