You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Traits sound like a Java "interface", i.e., a method contract, except somewhat complicated by multiple dispatch. For example, for a two argument signature, the contract could specify:
In practice, it's been sufficient to rely on duck typing, although it would be nice to have automatic checks. A nice thing about duck typing is that packages can define methods without even knowing about a trait, and interoperate by informal convention. Another potential feature would be automatically identifying whether a class conforms to a trait, without actually declaring it. Runtime checks could use that to fail early.
Maybe all I want is to be able to dynamically tell if an object has certain methods defined (possibly excluding default methods?). Then a trait is just a run-time check that an object implements all needed methods — the primary advantage being that you can deliver a clear error message at the start of a function, rather than erroring part way through because a required method is absent.
Ooops, yeah. Although I think my point is that the OO system doesn't need to define traits, as long as it provides an easy and efficient way to tell which methods are registered.
That's right. Traits would just make those checks more convenient, as the set of methods would defined in one place, and potentially checked in multiple places.
I guess the question is what happens if the trait involves multiple objects? Probably some parts of of the trait will only one argument and some parts would require two? But it feels like you could always break that into two traits, based on the number of dispatched arguments.
Should traits also be able to request that the method has specific optional arguments? That's probably starts to feel too esoteric.
Feels like the ability to assert that an object implements a set of methods is the biggest win, and since should be relatively easy to implement, it's worth it (even though it would be easy enough to roll your own as needed, it's better to standard in the object system).
And again it would be nice if classes could have traits, particularly virtual/abstract ones, because the assertion would be that any instance (of a concrete subclass) would have those methods.
Hmmm, I can't quite see how this would work. You can't declare that a class has a trait at class creation time, because you haven't yet declared any methods. And then if you're checking afterwards, why is it more useful to assert that a class has a trait rather than an object? What am I missing?
The assertion is that any instance of the class will satisfy the trait, i.e., it's a class-level contract, but the constraints are only checked on instances, at construction time. Btw, in principle you could do this with S4 using the validity function, so it's really just a convenience on top of the more general validation mechanism.
Oh, so you're saying that the trait would apply to an "abstract" parent class, requiring that the child classes all provide the necessary set of methods?
library(R7)
# Object can be used with for-loopfor_trait<- new_trait(c("length", "[["))
foo<- new_class("foo", constructor=function(...) {
check_trait(for_trait, new_object(...))
})
Other places we might want to use traits are in generics (to give early/clear error if some needed methods are not defined) and in properties (as a weaker assertion than a specific class).
@yutannihilation I don't think we have to worry about that problem because R7 uses generic function OO (i.e. methods belong to generics) not message passing OO (i.e. methods belong to classes).
The advantage of traits over interfaces is that traits can be added after class definition, and implementations can be provided by someone other than the class author (following the "orphan" rule that shouldn't definition an implementation for a trait unless you either created the trait or created the class). Registering the trait after construction would also make it possible to define the methods, and then we could check the trait constraints are met at definition-time time, not at run-time.
As well as using for type checks (e.g. in properties), it seems like it would be useful to be able to add methods for traits. This would imply a major change to dispatch since it would need to become dynamic: dispatch for a class would vary based on what traits had been defined for it.
Another way to think about a trait is as a dynamic union of all classes for which methods exist for a set of generics (really for specific arguments of specific generics). That would make it possible for someone writing a method to define a custom trait that meets the specific requirements of that method definition, and the system would automatically find all implementing classes and define methods for them like it would for a union. The complication is that it would need to be dynamic --- if a new or existing class comes to meet the requirements of the trait, the system would need to detect that and define the associated methods. Luckily there would be no performance hit at runtime; it would just require some extra logic when registering methods.
Maybe all I want is to be able to dynamically tell if an object has certain methods defined (possibly excluding default methods?). Then a trait is just a run-time check that an object implements all needed methods — the primary advantage being that you can deliver a clear error message at the start of a function, rather than erroring part way through because a required method is absent.
Activity
hadley commentedon Sep 29, 2020
Maybe all I want is to be able to dynamically tell if an object has certain methods defined (possibly excluding default methods?). Then a trait is just a run-time check that an object implements all needed methods — the primary advantage being that you can deliver a clear error message at the start of a function, rather than erroring part way through because a required method is absent.
lawremi commentedon Sep 29, 2020
Sounds like you're describing exactly what I described in the last couple sentences, and I agree that would be the most useful feature.
hadley commentedon Sep 29, 2020
Ooops, yeah. Although I think my point is that the OO system doesn't need to define traits, as long as it provides an easy and efficient way to tell which methods are registered.
lawremi commentedon Sep 29, 2020
That's right. Traits would just make those checks more convenient, as the set of methods would defined in one place, and potentially checked in multiple places.
hadley commentedon Oct 22, 2021
I think a trait would just be a list of methods, e.g.
And then you can assert that an object (or set of objects) has that trait
I guess the question is what happens if the trait involves multiple objects? Probably some parts of of the trait will only one argument and some parts would require two? But it feels like you could always break that into two traits, based on the number of dispatched arguments.
Should traits also be able to request that the method has specific optional arguments? That's probably starts to feel too esoteric.
Feels like the ability to assert that an object implements a set of methods is the biggest win, and since should be relatively easy to implement, it's worth it (even though it would be easy enough to roll your own as needed, it's better to standard in the object system).
lawremi commentedon Oct 23, 2021
And again it would be nice if classes could have traits, particularly virtual/abstract ones, because the assertion would be that any instance (of a concrete subclass) would have those methods.
hadley commentedon Oct 24, 2021
Hmmm, I can't quite see how this would work. You can't declare that a class has a trait at class creation time, because you haven't yet declared any methods. And then if you're checking afterwards, why is it more useful to assert that a class has a trait rather than an object? What am I missing?
lawremi commentedon Oct 28, 2021
The assertion is that any instance of the class will satisfy the trait, i.e., it's a class-level contract, but the constraints are only checked on instances, at construction time. Btw, in principle you could do this with S4 using the validity function, so it's really just a convenience on top of the more general validation mechanism.
hadley commentedon Oct 29, 2021
Oh, so you're saying that the trait would apply to an "abstract" parent class, requiring that the child classes all provide the necessary set of methods?
lawremi commentedon Oct 31, 2021
That's right.
hadley commentedon Nov 1, 2021
So something like this in essence:
hadley commentedon Jan 31, 2022
Good description of traits https://doc.rust-lang.org/book/ch10-02-traits.html — is more strict than what we can achieve because also specifies the input/output types of the methods.
Other places we might want to use traits are in generics (to give early/clear error if some needed methods are not defined) and in properties (as a weaker assertion than a specific class).
yutannihilation commentedon Feb 1, 2022
hadley commentedon Feb 2, 2022
@yutannihilation I don't think we have to worry about that problem because R7 uses generic function OO (i.e. methods belong to generics) not message passing OO (i.e. methods belong to classes).
yutannihilation commentedon Feb 3, 2022
Ah, thanks, makes sense. Considering the name collision is what can happen on defining a generic function, we don't need to worry about it here.
hadley commentedon Feb 18, 2022
Julia equivalent: https://docs.julialang.org/en/v1/manual/interfaces/
hadley commentedon Feb 26, 2022
The advantage of traits over interfaces is that traits can be added after class definition, and implementations can be provided by someone other than the class author (following the "orphan" rule that shouldn't definition an implementation for a trait unless you either created the trait or created the class). Registering the trait after construction would also make it possible to define the methods, and then we could check the trait constraints are met at definition-time time, not at run-time.
As well as using for type checks (e.g. in properties), it seems like it would be useful to be able to add methods for traits. This would imply a major change to dispatch since it would need to become dynamic: dispatch for a class would vary based on what traits had been defined for it.
Imagining what the code might look like:
Or maybe
In either case, registering a trait would have to mutate the class definition or some global class-trait registry.
hadley commentedon Mar 8, 2022
Interesting idea from Julia: https://www.ahsmart.com/pub/holy-traits-design-patterns-and-best-practice-book/#example_1_base_iteratorsize — traits are not baked in to the OO system, but are a pattern implemented on top of it.
lawremi commentedon Mar 14, 2022
I am confused by the code above, is it supposed to be:
Instead of:
?
Another way to think about a trait is as a dynamic union of all classes for which methods exist for a set of generics (really for specific arguments of specific generics). That would make it possible for someone writing a method to define a custom trait that meets the specific requirements of that method definition, and the system would automatically find all implementing classes and define methods for them like it would for a union. The complication is that it would need to be dynamic --- if a new or existing class comes to meet the requirements of the trait, the system would need to detect that and define the associated methods. Luckily there would be no performance hit at runtime; it would just require some extra logic when registering methods.
sounkou-bioinfo commentedon May 5, 2026
Mostly derivative of this issue (and this specific point) among other things, interfaces following Golang mental model and "traits" implemented with S7 here https://github.com/sounkou-bioinfo/s7contract