dibyendumajumdar / ravi

Ravi is a dialect of Lua, featuring limited optional static typing, JIT and AOT compilers
http://ravilang.github.io/
Other
1.16k stars 60 forks source link

Annotated function arguments and `nil` values #228

Open snoopcatt opened 3 years ago

snoopcatt commented 3 years ago

Hello. Let's summarize our discussion on that.

Currently we have strange behaviour of annotated function arguments. With some argument types (number, integer, number[], integer[], table) you can't pass nil, you will get a type error. But with other types -- string, closure, userdata -- you can pass nil without any errors.

Example:

function test(a: number)
    print(a)
end
test()

ravi: t.lua:0: TOFLT: number expected

Another example:

function test(a: closure)
    print(a)
end
test()

nil (no error thrown)

I think behaviour of annotated arguments should be unified. Most obvious solution is to make all arguments with specified type -- mandatory. (it is already that in JIT mode: #225)

In case when argument must be optional -- we have any type (or just omit annotation) as for now.

dibyendumajumdar commented 3 years ago

I have pushed some changes to make it consistent between the JIT and the interpreter. For now, this continues the existing model in the VM that NIL is valid value for Lua types - the Ravi add-on types do not allow NIL.