Skip to content
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

Open
ggoraa opened this issue May 9, 2023 · 3 comments
Open

Protocols #655

ggoraa opened this issue May 9, 2023 · 3 comments
Labels
feature request New feature or request

Comments

@ggoraa
Copy link

ggoraa commented May 9, 2023

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:

local trait MyTrait
    foo(bar: number): string
end

local record MyRecord end

function MyRecord.new(): MyRecord
    -- creates a new MyRecord instance
end

implement MyTrait for MyRecord
    function foo(bar: number): string
        -- do something
    end
end

local function baz<T: MyTrait>(arg: T)
    arg.foo(0) -- perfectly legal
end

-- and multiple traits can be chained like this:

local function bazz<T: MyTrait + MyOtherTrait>(arg: T)
    arg.foo(0) -- perfectly legal
end

local recordInstance = MyRecord.new()
baz(recordInstance) -- perfectly legal
baz(1) -- nein, integer doesn't implement MyTrait

This example is basically a ripoff of how Rust did it
Can this be a good thing to be added to Teal?

@lenscas
Copy link
Contributor

lenscas commented May 9, 2023

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.

@ggoraa
Copy link
Author

ggoraa commented May 9, 2023

Understood, then how the Teal team sees how a problem like this can be solved in Teal?
I suggested a trait system so that logic can take in more generalised data instead of concrete types, which improves code portability and flexibility

@ggoraa ggoraa changed the title Traits Generics: Type Constraints (Traits) May 9, 2023
@ggoraa ggoraa changed the title Generics: Type Constraints (Traits) Traits May 11, 2023
@ggoraa ggoraa changed the title Traits Traits/Protocols May 14, 2023
@ggoraa ggoraa changed the title Traits/Protocols Protocols May 14, 2023
@ggoraa
Copy link
Author

ggoraa commented May 14, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants