Traits from structs
In terms of a feature, would be interesting to reference an existing struct as a trait, maybe with the *operator or something
12 Replies
Can you give an example snippet of code to illustrate the idea?
Maybe something like
struct Example:
fn add_one(a: Int) -> Int:
return a + 1
struct ExampleTwo(*Example):
…
Where exampletwo has to implement add_one with the same signature, but doesn’t inherent the method from Example
sorry code blocks aren’t working for me
What's the advantage to this idea over just creating a new trait normally? I can't think of anything besides a little saved typing
the main argument I see is for much larger structs, a little saved typing goes a long way, just would be a nice qol change imho
i just think saving a little typing is better than not
Is this something related to Abstruct Types found in object object programming languages?
https://en.wikipedia.org/wiki/Abstract_type
Abstract type
In programming languages, an abstract type (also known as existential types) is a type in a nominative type system that cannot be instantiated directly; by contrast, a concrete type can be instantiated directly. Instantiation of an abstract type can occur only indirectly, via a concrete subtype.
An abstract type may provide no implementation, o...
I mean, traits on their own are already an abstract type, so technically yes but thats not really the question
Traits are just definitions, not implementations. Abstract class (or types) provides mix of implementation as well as definitions!
Congrats @Aamir, you just advanced to level 4!
I do not mean this, i mean generating a trait from a struct, getting rid of the implementations
I see!
I think it's going to be very difficult (for the compiler) to tell exactly which part you want to extract as the trait, like do we want the generated trait to have all the trait bound on the struct itself; for a struct
S
, should a method taking an S
argument treated as taking S
or Self
; how does that work with extension, etc etc. We may end up needing all kinds of annotations at which point it's better we wrote the trait explicitly by hand.I say just let the notation mean, whatever function inherits this trait must implement all functions implemented in the struct, no extension needed
If a struct inherits other functions from a different struct, or is bound to other traits, that holds true for that definition