New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Protocols #655
Comments
one of the goals of teal is to stay close to lua. Considering that lua doesn't have traits I don't think that adding them to teal is good. Teal needs interfaces and the like though, ideally with some way for types to have them be implemented structurally rather than only nominally, similarly how TS does things. |
Understood, then how the Teal team sees how a problem like this can be solved in Teal? |
Changed the example: global protocol MyProtocol
foo: function(self: Self): string
end
local record MyRecord end
local function MyRecord.new(): MyRecord
-- creates a new MyRecord instance
end
extension MyRecord: MyProtocol
function foo(self: Self): string
return "foo"
end
end
-- first usage variant
local function baz<T: MyProtocol>(arg: T)
arg:foo() -- perfectly legal
end
-- second usage variant
local function baz(arg: any MyProtocol)
arg:foo() -- perfectly legal
end
-- and multiple protocols can be chained like this:
local function baz<T: MyProtocol + MyOtherProtocol>(arg: T)
arg:foo() -- perfectly legal
end
local recordInstance = MyRecord.new()
baz(recordInstance) -- perfectly legal
baz(1) -- nein, integer doesn't implement MyProtocol
|
I have just discovered Teal and I instantly fell in love with it, because of how it made Lua miles better with type safety. But I can't find one feature that I really want to exist, and it is traits. In general, I see them basically the same as Rust traits, which are very powerful. Why add them? Well, instead of fiddling around with inheritance and subclassing and stuff you just add a trait to a record. Example:
This example is basically a ripoff of how Rust did it
Can this be a good thing to be added to Teal?
The text was updated successfully, but these errors were encountered: