lua-users home
lua-l archive

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


Ravi = SOL in unexpected ways :-)

Below @table is unary operator that asserts the type of its operand.

> function x(a) local t: table = @table a; return a[1]; end; x {42}
> ravi.dumplua(x)

function <stdin:1,1> (5 instructions at 0000025159A75090)
1 param, 3 slots, 0 upvalues, 2 locals, 1 constant, 0 functions
        1       [1]     TOTAB           0
        2       [1]     MOVE            1 0
        3       [1]     GETTABLE        2 0 -1  ; 1
        4       [1]     RETURN          2 2
        5       [1]     RETURN          0 1
constants (1) for 0000025159A75090:
        1       1
locals (2) for 0000025159A75090:
        0       a       1       6
        1       t       3       6
upvalues (0) for 0000025159A75090:
> function x(a) local t: table = @table a; return a[1]; end; print(x {42})
42
>


On 9 November 2015 at 22:00, Dibyendu Majumdar <mobile@majumdar.org.uk> 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