traits

How to declare traits as taking implicit “constructor parameters”?

青春壹個敷衍的年華 提交于 2019-11-30 10:36:37
问题 I'm designing a class hierarchy, which consists of a base class along with several traits. The base class provides default implementations of several methods, and the traits selectively override certain methods via abstract override , so as to acts as stackable traits/mixins. From a design perspective this works well, and maps to the domain so that I can add a filtering function from here (one trait) with a predicate from here (another trait) etc. However, now I'd like some of my traits to

Why can't a class extend traits with method of the same signature?

风流意气都作罢 提交于 2019-11-30 10:26:37
问题 Why do I get the error below? How to workaround it? I assumed that since A and B compile to (interface,class) pairs, it's a matter of choosing the right static method call to implement when compiling C. I would expect the priority to be according to order. scala> trait A { def hi = println("A") } defined trait A scala> trait B { def hi = println("B") } defined trait B scala> class C extends B with A <console>:6: error: error overriding method hi in trait B of type => Unit; method hi in trait

Javascript Traits Pattern Resources

空扰寡人 提交于 2019-11-30 10:06:33
Could anyone recommend good resources for using traits in javascript? After some searching I mainly find articles about libraries that provide traits functionality, but I was curious about best practices on how to implement traits without a library. I came across this post on SO, are there any other approaches? Traits in javascript Any real world examples would be welcome as well. Thanks. I would suggest something simple, along the lines of: Let traits be defined as standard JavaScript objects. var equalsTrait = { eq: function(obj) { return this == obj }, neq: function(obj) { return ! this.eq

How to Box a trait that has associated types?

≯℡__Kan透↙ 提交于 2019-11-30 09:22:09
问题 I'm very new to Rust so I may have terminology confused. I want to use the hashes crates to do some hashing and I want to dynamically pick which algorithm (sha256, sha512, etc.) to use at runtime. I'd like to write something like this: let hasher = match "one of the algorithms" { "sha256" => Box::new(Sha256::new()) as Box<Digest>, "sha512" => Box::new(Sha512::new()) as Box<Digest> // etc... }; I sort of get that that doesn't work because the associated types required by Digest aren't

Unit testing helper or non-interface traits in Scala

只谈情不闲聊 提交于 2019-11-30 08:50:56
This question is about dealing with testing of classes which mix in non-interface traits, that is traits containing some functionality. When testing, the class functionality should be isolated from the functionality provided by the mix-in trait (which is supposedly tested separately). I have a simple Crawler class, which depends on a HttpConnection and a HttpHelpers collection of utility functions. Let's focus on the HttpHelpers now. In Java , HttpHelpers would possibly be a utility class, and would pass its singleton to Crawler as a dependency, either manually or with some IoC framework.

Overriding Doctrine Trait Properties

僤鯓⒐⒋嵵緔 提交于 2019-11-30 08:12:05
I know you can override a trait method by declaring it in your class, I was curious if was possible to over ride a trait Property the same way. Is this safe to do? Its not in the Documentation so I am hesitant to implement this. From the Documentation An inherited member from a base class is overridden by a member inserted by a Trait. The precedence order is that members from the current class override Trait methods, which in turn override inherited methods. http://php.net/manual/en/language.oop5.traits.php You cannot override a trait's property in the class where the trait is used. However,

Why do I get the error “the trait `Foo` is not implemented for `&mut T`” even though T implements the trait?

倖福魔咒の 提交于 2019-11-30 07:34:29
问题 I have this source: pub fn draw<G, C>(&self, font: &mut C, draw_state: &DrawState, transform: Matrix2d, g: &mut G) where C: CharacterCache, G: Graphics<Texture = <C as CharacterCache>::Texture>, { self.properties.draw( self.text.as_str(), &mut font, &draw_state, transform, g, ); } And the error the trait bound `&mut C: graphics::character::CharacterCache` is not satisfied (the trait `graphics::character::CharacterCache` is not implemented for `&mut C`) The only aspect of C that is defined is

Does Objective-C support traits/mixins?

半腔热情 提交于 2019-11-30 03:49:38
Are there any techniques for emulating traits or mixins in Objective-C? In Scala, for example, I can do something like this: trait ControllerWithData { def loadData = ... def reloadData = ... def elementAtIndex = ... } trait ControllerWithStandardToolbar { def buildToolbar = ... def showToolbar = ... def hideToolbar = ... } class MyTableController extends ControllerWithData with ControllerWithStandardToolbar { def loadView = { super.loadView loadData buildBar } } It's basically a way to combine (or mix in) multiple pieces of functionality into a single class. So right now I have kind of an all

Is an is_functor C++ trait class possible?

微笑、不失礼 提交于 2019-11-30 02:09:42
How can I deduce statically if an argument is a C++ function object (functor)? template <typename F> void test(F f) {} I tried is_function<F>::value , but this doesn't work. It also seems there is no is_functor trait, so perhaps it's not possible. I appear to be only looking for a specific member function, in this case the function call operator: F::operator() . It is possible to create such a trait, with two restrictions: For the compiler, a free function is something fundamentally different from a class functor that overloads operator() . Thus we have to treat both cases seperately when

How to use stackable trait pattern with Akka actors?

假如想象 提交于 2019-11-30 00:13:39
I'm trying to implement a Pub/Sub trait to mix into other akka actors using a stackable trait. Here is what I came up with: trait PubSubActor extends Actor { abstract override def receive = super.receive orElse { case Subscribe(topic) => /* ... */ case Publish(topic, msg) => /* ... */ } } class MyActor extends Actor with PubSubActor { override def receive = { case SomeMessage(a, b, c) => /* ... */ } } At which point, the compiler throws back an error: error: overriding method receive in trait MyActor... method receive needs `abstract override' modifiers. Can you explain to me why this isn't