Will the special methods slowly transition to traits?

Will the special methods slowly get replaced by traits to allow for static analysis, optimizations, more safety, parameterized functions discriminating by traits etc. ? e.g. std::ops Traits like Add for __add__, Mul... in Rust ?
9 Replies
sora
sora13mo ago
As much as I like the type constrains, I don't think simple traits like Mul are going to be very useful especially in a language with overload like Mojo. Or I'm trying to say that the language probably shouldn't enforce one to implement a trait Mul in order for the notation * to work a la Rust. We should distinguish the need for ad-hoc polymorphism from the need for notation, granted sometimes those needs align. For instance, do you want sum to work on any type that implements a __add__ and __zero__ or on a type that is a Monoid? I'm leaning on the latter, meaning maybe we should provide Add, Mul etc. but people should really think if they want the conformance (when two operations interoperate, like add and mul) or they just want the syntax sugar. Plus, allowing both feel more gradual in the sense of gradual typing. For the same reason, it is probably also a case against implicit traits (syntactic behaviour vs structural behaviour in the mathematical sense).
Théo Pomies
Théo PomiesOP13mo ago
I get you, but without these traits, how can I make a parametric function that restricts its arguments to Addable types? If I use structs and fn, I want some static guaranties that I can perform this operation on the arguments. I want that to raise an error at compile time, not runtime, otherwise I'd use the pythonic counterpart
sora
sora13mo ago
That’s exactly what I’m saying: there are cases where you don’t want generic functions to work on them, in which case, they probably shouldn’t add a addable constrain and just use the special function. So I propose providing both.
Jack Clayton
Jack Clayton13mo ago
Hi @Théo Pomies yes they will. For @sora's point we're still discussing how Mojo's generics will evolve. There is a lot of prior design to consider in this space, and we want to avoid the complexity other modern languages have introduced. We'll make sure to keep the community in the loop 🙂
Kushagra
Kushagra13mo ago
That's a HUGE RESPECT !!! Because making things more complex than they need to be, is something current generation is getting very good at.
Théo Pomies
Théo PomiesOP13mo ago
Great, thanks for the updates
sora
sora13mo ago
Just to confirm: we will need associated types to make these work, right?
Jack Clayton
Jack Clayton13mo ago
Yes I believe we'll need to introduce associated aliases to traits
Want results from more Discord servers?
Add your server