traits

How to get list of traits that were mixed in the specified class?

此生再无相见时 提交于 2020-01-03 12:31:33
问题 And more specific example: abstract trait A trait B extends A trait C extends A How to check what traits that extend trait A (it can be from 0 to many) were mixed in specified class? 回答1: How about a hybrid of the other answers abstract trait A //interested in this one trait B extends A //and this one trait C extends A //this one too trait D //don't care about this one though val x = new A with B with D x.getClass.getInterfaces.filter(classOf[A].isAssignableFrom(_)) returns Array[java.lang

How to get list of traits that were mixed in the specified class?

房东的猫 提交于 2020-01-03 12:31:13
问题 And more specific example: abstract trait A trait B extends A trait C extends A How to check what traits that extend trait A (it can be from 0 to many) were mixed in specified class? 回答1: How about a hybrid of the other answers abstract trait A //interested in this one trait B extends A //and this one trait C extends A //this one too trait D //don't care about this one though val x = new A with B with D x.getClass.getInterfaces.filter(classOf[A].isAssignableFrom(_)) returns Array[java.lang

Delaying trait initialization

帅比萌擦擦* 提交于 2020-01-02 04:49:26
问题 I need a smart mechanism for component composition which allows mixed in traits to initialize after the composed component. The following throws a NullPointerException : class Component { def addListener(pf: PartialFunction[Any, Unit]) {} } trait DynamicComponent { protected def component: Component component.addListener { case x => } } class Foo extends DynamicComponent { protected val component = new Component } new Foo // -> NullPointerException The following things are not options for me:

Is there a trait supplying `iter()`?

Deadly 提交于 2020-01-02 03:40:28
问题 Is there in Rust a Trait which supplies the iter() method? I only found the trait IntoIterator , which supplies into_inter() . Just to be clear here: I do not want the Iterator trait, which supplies next() , but a trait which supplies iter() . [ sidenote: Sometimes I'm very confused by the Rust libs. Iterator supplies next() , but IntoIterator supplies into_iter() (not supplying next() and for convention with moving), while IntoIter is a struct, that implements the Iterator trait (moving

Symfony container traits

走远了吗. 提交于 2020-01-02 02:56:05
问题 Strange problem, I have controller which uses \Symfony\Component\DependencyInjection\ContainerAwareTrait class MainController { use \Symfony\Component\DependencyInjection\ContainerAwareTrait; /** * @Route("/", name="_index") * @Template() */ public function indexAction() { var_dump($this->container); return array(); } } but result is NULL. Tried on: Symfony 2.5.* MAMP 3.0 PHP 5.4 5.5 My searches have not helped me. I think the solution is easy. Any ideas how to trace this error? UPD: When i

Scala by Example - trait type parameter with context bounds mistake?

爷,独闯天下 提交于 2020-01-01 08:46:11
问题 Reading the Scala by Example book and there is this example when Martin explains type bounds on page 54: trait Set[A <: Ordered[A]] { def incl(x: A): Set[A] def contains(x: A): Boolean } and trait Set[A <% Ordered[A]] ... further on page 55. He also says that the <:/<% is the only change required for trait Set in order to demonstrate the type bounding possibilities. However, when I repeat the example with my own code, the IDE complains that traits may NOT have view bounds, only type bounds.

Specifying generic parameter to belong to a small set of types

走远了吗. 提交于 2019-12-31 04:05:22
问题 Is it possible with to constrain a generic parameter to be one of the select few types without figuring out what traits precisely define those type? e.g. impl<T> Data<T> where T == u32 || T == u64 Sometimes it's tedious to figure out what all traits to add to where to get the types you want, and sometimes one wouldn't want to allow a type even when it makes syntactic sense because of semantics. 回答1: You could use a marker trait for the types you want to support: trait DataSupported {} impl

Scala extends double arrow

不羁岁月 提交于 2019-12-31 02:01:07
问题 I was looking at the Anorm source code and for the RowParser trait it has the declaration: trait RowParser[+A] extends (Row => SqlResult[A]) { ... } I was curious what type (Row => SqlResult[A]) is? I haven't been able to find an explanation of the syntax anywhere. 回答1: This is syntactic sugar for Function1 which is the same as this: trait RowParser[+A] extends Function1[Row, SqlResult[A]] If you look further in the Anorm source you'll see that when the trait is implemented it has to define

How can I enforce equality of two associated type parameters of traits?

删除回忆录丶 提交于 2019-12-31 01:44:33
问题 I have a function f which takes two arguments of the same type, and a function g which takes two arguments of different types, but both types have to store the same value, so that g can call f with the values contained in the arguments to f . I currently implemented something like this: fn f<T>(a: T, b: T) {} trait A { type A; fn getter(&self) -> Self::A; } fn g<T: A, U: A>(a: T, b: U) { f(a.getter(), b.getter()) } What do I have to add to the definition of g to make it work? 回答1: I found a

What does it mean for a trait to have a lifetime parameter?

孤人 提交于 2019-12-30 10:57:06
问题 I understand how lifetime parameters apply to functions and structs, but what does it mean for a trait to have a lifetime parameter? Is it a shortcut to introduce a lifetime parameter to its methods, or is it something else? 回答1: If you have a trait with a lifetime bound, then implementors of the trait can participate in the same lifetime. Concretely, this allows you to store references with that lifetime. It is not a shortcut for specifying lifetimes on member methods, and insanity and