M
Modular7mo ago
benny

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
Joe
Joe7mo ago
Can you give an example snippet of code to illustrate the idea?
benny
benny7mo ago
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
Ryulord
Ryulord7mo ago
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
benny
benny7mo ago
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
Aamir
Aamir7mo ago
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...
benny
benny7mo ago
I mean, traits on their own are already an abstract type, so technically yes but thats not really the question
Aamir
Aamir7mo ago
Traits are just definitions, not implementations. Abstract class (or types) provides mix of implementation as well as definitions!
ModularBot
ModularBot7mo ago
Congrats @Aamir, you just advanced to level 4!
benny
benny7mo ago
I do not mean this, i mean generating a trait from a struct, getting rid of the implementations
Aamir
Aamir7mo ago
I see!
sora
sora7mo ago
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.
benny
benny7mo ago
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
Want results from more Discord servers?
Add your server