lua-users home
lua-l archive

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

On Mon, Nov 9, 2015 at 2:00 PM, Dibyendu Majumdar
<> wrote:
> Hi,
> As I progress with Ravi I often wish there was a way to perform type
> assertion or coercion in the language. For example:
> local t = { 1 }
> local i: integer = t[1]
> Above fails in Ravi as the compiler cannot know that the value
> returned from t[1] is an integer.
> It would be nice to be able to say:
> local i: integer = t[1] as integer
> Or some such like.
> Language support is essential for performance reasons; Ravi already
> supports type coercion of function return values. So following already
> works.
> function asinteger(a) return a end
> local i: integer = asinteger(t[1])
> Problem is this is bad for performance.
> Now here is a hypothetical question - if Lua had been statically typed
> language - what would the ideal expression be for this kind of type
> coercion?
> Regards
> Dibyendu

Personally I'd go with asinteger() (or perhaps cast.integer(), to
limit the namespace pollution to a single keyword) so that the grammar
doesn't need modified, and then special-case the function invocation
as some kind of intrinsic instead of an actual function call.

/s/ Adam