andremm / typedlua

An Optional Type System for Lua
563 stars 53 forks source link

Indexing array of thing gives (thing?) ? #105

Open Zash opened 7 years ago

Zash commented 7 years ago

This is things.tl:

local interface thing
    x:string
end

local things:{thing} = {};

for i = 1, #things do
    local t:thing = things[i];
    print(t.x);
end

This is the output of tlc things.tl:

things.tl:10:8: type error, attempt to assign 'thing?' to 'thing'
 local t:thing = things[i];
       ^
things.tl:11:8: type error, attempt to index 'thing?' with '"x"'
 print(t.x);
       ^

Why is this? How do I tell it that I'm really sure that it's not out of bounds?

Veltas commented 7 years ago

I recommend aliasing assert like suggested here: https://github.com/andremm/typedlua/issues/89#issuecomment-233391358

From that issue you'll see that there is a better way of doing this planned, but for now this is what we have.