Replies: 4 comments 1 reply
|
To use some more formal terms records are nominal types (i.e. even if two records have the same fields they arent compatible due to them having different 'names') and interfaces are structural types. Another thing to note is the timeline of teal's So while they are similar they serve slightly different purposes. Maybe if interfaces were implemented first or earlier on records wouldn't be a thing as I'd argue people (mostly) think of tables as structural anyway. |
|
When writing .d.tl files, when using a record you are promising that a table with the given name of the type exists. But that obviously doesn't have to be the case if you are using the .d.tl file to write for example bindings to a library written in rust rather than teal. |
|
Records are tables in Lua, they have an implementation, whereas interfaces are abstract - they only exist in Teal. Teal decided early on it would not implement record inheritance because that would mean providing an implementation of inheritance in Lua. Teal does not do that to remain compatible with the ecosystem of Lua object libraries. |
|
Thank you, this is really helpful! |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone,
I am trying to switch to Teal more seriously these days and I am struggling to understand how exactly records and interfaces are different. I understand that records can inherit after interfaces but interfaces can also inherit after interfaces—including multiple interfaces at the same time. Why not just instantiate an object straight from an interface instead of creating another record type?
Consider:
The only thing I can think of that can be done with a record that cannot be done with an interface is defining a pre-initialized method. So, theoretically, to have a fully-fledged class-like object in the Python sense, for instance, you would need to do it like that:
So are records just interfaces with functions and without inheritance?
What then is the purpose of function declarations in interfaces, like this:
And then why not just have interfaces with functions and forget about records? Is method inheritance somehow problematic that we need a whole separate type just to make it possible?
I must be missing something. Help!
All reactions