M
Modular•3mo ago
tkeitt

Parameterized function definitions in traits

Is this possible?
trait X:
fn x[Y: AnyType](self, y: Y) -> Y:
pass
trait X:
fn x[Y: AnyType](self, y: Y) -> Y:
pass
4 Replies
mad alex 1997
mad alex 1997•3mo ago
Yes. Here is an example https://github.com/Mojo-Numerics-and-Algorithms-group/NuMojo/blob/main/numojo/traits/backend.mojo. The main limitations of trait that I have run into are you can't parameterize at the trait level trait ball[color:Color]:, nor can you require fields to exist in conformant structs.
tkeitt
tkeitt•3mo ago
Ah. I just needed to update to the latest nightly. Edit: Actually, that was not the fix. I was trying to specialize in the type definition, but specialization occurs at the point of invocation, so the trait requirements have to be duplicated exactly in the type.
Three chickens in the green bag
It does 😄
Three chickens in the green bag
If you ever wonder, just run it in the playground: https://docs.modular.com/mojo/playground
Want results from more Discord servers?
Add your server