traits

Generic design mixed with curiously recurring template pattern. C++

≡放荡痞女 提交于 2019-12-07 12:08:19
问题 Consider this kind of problem. I have a Base class and three classes derived from Base . For instance: DerivedA , DerivedB and DerivedC . Each derived class has its unique container. Hence DerivedA has std::vector<int> , DerivedB has std::set<int> and DerivedC has std::map<int, std::string> . And I want an interface in Base to access the container of derived class on which it is currently pointed to. Base* d1 = new DerivedA; for(std::vector<int>::iterator iter = d1->begin(); iter != d1->end()

AbstractMethodError when mixing in trait nested in object - only when compiled and imported

∥☆過路亽.° 提交于 2019-12-07 10:15:09
问题 Consider the following code: object Impls { trait ConcreteImpl { type Foo = Int def foo = 1 } } trait Base { type Foo def foo: Foo } I am interested in the expression (new Base with Impls.ConcreteImpl).foo When I paste the above code in the REPL, or run it in a worksheet, and then try the expression -- no problem, it prints res0: Int = 1 , as expected. When I put the code in a file, compile it with scalac , and then use the compiled class files from the REPL in the same directory, I get the

Method inheritance in immutable classes

老子叫甜甜 提交于 2019-12-07 09:53:35
问题 I am stumbling on something that I hope is a bit of a basic issue. Probably its because I am new to Scala, and probably I am still missing some important concepts. I am trying to program in an FP fashion, and data classes which do not need to have a mutable state are immutable, with some transformation methods to create new objects to update them if needed. However, I am struggling when it comes to maintaining the return types of this method when I have traits and general inheritance in place

Methods in trait become volatile methods when mixed in concrete classes in 2.9.0-1 but not 2.8.1

梦想与她 提交于 2019-12-07 09:18:10
问题 I noticed this breaking (for me using it with OGNL) change in 2.9.0-1: I find that, in 2.9, methods declared in a trait become volatile when mixed in a class: Example in 2.9.0-1 import java.lang.reflect.Modifier trait SuperTrait { def getKnoll = "Kanutten" } class KlassWithKnoll extends SuperTrait { def getKnall = "Mars" } val qsc = classOf[KlassWithKnoll] val knollGetter = qsc.getDeclaredMethod("getKnoll") println("isVolatile: " + Modifier.isVolatile(knollGetter.getModifiers())) This prints

c++ check for nested typedef of a template parameter to get its scalar base type

≯℡__Kan透↙ 提交于 2019-12-07 07:43:31
问题 Consider the exponential smoother template class below. This class is for smoothing/filtering sequential data exponentially (see update method). Elemtype might be an vector and Floattype is usually a scalar. E.g. ExponentialSmoother<Eigen::Vector2f, float> x(0.1, Vector2f(0.5, 0.5)); In this example the second template parameter Floattype could be avoided because Eigen's Matrix class contains a nested typedef to get the scalar base type: Vector2f::Scalar It is also reasonable to instantiate

How to initialize trait's vals in subtrait?

这一生的挚爱 提交于 2019-12-07 06:30:44
问题 I tried using an abstract val in a trait to initialize another value. I got a NullPointerException . I boiled the behaviour down to a minimal test case: trait MessagePrinter { val message: String println(message) } class HelloPrinter extends MessagePrinter { val message = "Hello World" } val obj = new HelloPrinter() println(obj.message) This little program yields the following result: null Hello World I was under the impression that a val may never change. Is this expected behaviour or is it

PHP Class Using Same Name as Trait Function

只愿长相守 提交于 2019-12-07 04:19:00
问题 I have the following code as a sample. trait sampletrait{ function hello(){ echo "hello from trait"; } } class client{ use sampletrait; function hello(){ echo "hello from class"; //From within here, how do I call traits hello() function also? } } I could put all the details as to why this is necessary but I want to keep this question simple. Extending from the class client is not the answer here due to my particular situation. Is it possible to have a trait have the same function name as the

How to solve “Implementation restriction: trait … accesses protected method … inside a concrete trait method.”

纵饮孤独 提交于 2019-12-07 03:30:36
问题 A Java library class I'm using declares protected getPage(): Page { ... } Now I want to make a helper Scala mixin to add features that I often use. I don't want to extend the class, because the Java class has different subclasses I want to extend at different places. The problem is that if I use getPage() in my mixin trait , I get this error: Implementation restriction: trait MyMixin accesses protected method getPage inside a concrete trait method. Is there a solution how to make it work,

Using traits with a factory

*爱你&永不变心* 提交于 2019-12-07 03:13:09
问题 I'm currently discovering scala and I was wondering if I could use traits with a factory. I tried this : abstract class Foo { ... } object Foo { def apply() = new Bar private class Bar extends Foo { ... } } Foo() with MyTrait // Not working I guess it's because with must be preceded by new . So is there any way to do this ? Thank you 回答1: No it is too late, the instance is already created when the apply() method returns. What you can do is using the traits inside the factory method. The code

How can I fire a Traits static event notification on a List?

风流意气都作罢 提交于 2019-12-07 01:50:51
问题 I am working through the traits presentation from PyCon 2010. At about 2:30:45 the presenter starts covering trait event notifications, which allow (among other things) the ability to automatically call a subroutine any time a trait has changed. I am running a modified copy of the example he gave... In this trial, I am trying to see whether I can fire a static event whenever I make a change to volume or inputs . from traits.api import HasTraits, Range, List, Float import traits class