mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[Feature Request] Decorator to control implicit conformance to trait

Open soraros opened this issue 7 months ago • 1 comments

Review Mojo's 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.

soraros avatar Jul 13 '24 21:07 soraros