lua-users home
lua-l archive

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


On Tue, Jun 4, 2019 at 10:52 PM Dibyendu Majumdar wrote:

Now, the issue with a dynamic language like Lua is that you don't know
the return types of functions because functions are values that may
change over time. Also interoperability with dynamic types requires
you to have the ability to sometimes convert (or assert) a type is a
particular one. When trying to implement this I found that
grammatically it was not simple to do this, unless I used a special
character to introduce the type. So then I chose to use '@' to signify
a cast operator. So you can write:

local n: number = @number math.sqrt(5.0)

The '@number' is a unary operator that binds to the right and asserts
that whatever _expression_ is there can be converted to the required
type. Else an error is raised.





Why do you need additional operator "@number"?
IMO, assignment to a typed variable must check value's type in runtime.
JIT-compiled code may have this check removed if value's type could be proven (by code analysis) to be always correct.