Type independent traits
Any ways to make the example code work? Would be nice to be able to implement custom traits for abstract classes
9 Replies
I also tried
The code throws this error:
I don't think it's possible
@benny I think what you want is a generic trait, i.e. the following:
Mojo doesn't support this yet, but it's in the works.
sort of, but i was thinking generic on a function level, where each function can type in different parameters, rather than generic on a struct level
I’m not sure what distinction you’re making. Your struct can inherit from that trait multiple times with a different parameter, if you want to have multiple
__contains__
functions.
There’s also a notion of “associated types” (see Rust and Swift) which avoids the need for the trait to be parametrised.Sometimes you need both to make a nice api, and good example would be some
HasAdd
trait, where LHS
is generic, and Output
is an associated type.
IMO, this is why inherit is bad terminology for traits.
Could you show some code as your intended usage pattern?Ah, then you most definitely want associated type, and kinda have to wait.
This worked in the previous Mojo version with parameter input, but stopped working in the new version, so I had to remove it for Lightbug 🥲