Replies: 1 comment
-
Related to #1431. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The current (Mojo 0.6.1) trait system forces to pick just one trait for any given type parameter, not allowing to specify multiple traits.
Example:
CollectionElement
inherits multiple traits and still there is feature request forComparableCollectionElement
(#1526). Similarly, someone will wantHashableCollectionElement
and someone will wantStringableCollectionElement
andStringableComparableCollectionElement
and so forth. Each collection implementation will have to choose one of ever increasing set of traits. The trait system will face the same consequence of C++/Java/C# standard library having to define a huge hierarchy of trait inheritance where any possible combinations of traits must be defined ahead of time.On the other hand, Lean allows multiple traits (typeclass instances) for a given type parameter (https://lean-lang.org/lean4/doc/typeclass.html).
In Lean syntax:
Translated to imaginary Mojo syntax:
Extensions
This idea is orthogonal to the extension mechanism. However, extension will be the most important use case and I want to show the idea applies to it as well.
Example: We'd like make
DynamicVector[T]
to be stringable, wheneverT
is Stringable & CollectionElement. And we don't want to create StringableDynamicVector for that or make all DynamicVectors to be Stringable.In Lean syntax,
Translated to imaginary Mojo syntax:
Other languages
Rust and Haskell allow to compose traits as well with different syntax.
In Rust:
(from https://doc.rust-lang.org/rust-by-example/generics/where.html)
In Haskell:
(from https://wiki.haskell.org/Multi-parameter_type_class)
Rust-style composition syntax would work with Mojo.
Beta Was this translation helpful? Give feedback.
All reactions