mojo
mojo copied to clipboard
[Feature Request] Decorator to control implicit conformance to trait
Review Mojo's priorities
- [X] I have read the roadmap and priorities and I believe this request falls within the priorities.
What is your request?
Similar to the @implicit_conversion
, but for traits.
@implicit_conform # placeholder name
trait Copyable:
fn __copyinit__(...): ...
trait T:
fn f(...): ...
fn g(...): ...
struct S: # is `Copyable` but not `T`
fn __copyinit__(...): pass
What is your motivation for this change?
As of now, traits are structural, meaning types conform to them implicitly as long as all the methods are implemented. This is useful; however, it blocks these modes of use:
- We can't have marker traits (traits with no methods) anymore. They are useful type-level programming tool. For example, consider the following marker trait in Rust:
trait Serializable {}
This marker trait can be used to indicate that a type can be serialized, even though it doesn't require any methods to be implemented.
- When we support default implementations, every type will automatically acquire all the methods, which may or may not be what we want.
Any other details?
N/A.