teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.02k stars 101 forks source link

Can a variable be assigned to it's type? #739

Closed Andre-LA closed 4 months ago

Andre-LA commented 4 months ago

(note: I'm testing the next, but Teal Playground doesn't accuse an error either)

While trying interfaces, I found by accident that this code is accepted:

local record Foo
   x: integer
end

local x: Foo = Foo

print(Foo, x)

-- tl run test.tl 
-- table: 0x558977bee240    table: 0x558977bee240
Frityet commented 4 months ago

if you use a record like that, it gets turned into a table, you can check it out by doing tl gen test.tl

hishamhm commented 4 months ago

Yes, this is on purpose, because of record definitions being used as table prototypes (e.g. function MyRecord:my_method, MyRecord.my_static_field, etc.)