M
Modular•5mo 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•5mo 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
tkeittOP•5mo 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.
Moosems / Three chickens
It does 😄
Moosems / Three chickens
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