traits

case class and traits

I want create a special calculator. I think that case class is a good idea for operations: sealed class Expr case class add(op1:Int, op2:Int) extends Expr case class sub(op1:Int, op2:Int) extends Expr case class mul(op1:Int, op2:Int) extends Expr case class div(op1:Int, op2:Int) extends Expr case class sqrt(op:Int) extends Expr case cla...

type to int mapping

I have two c++ programs that need to have a map type -> int that is known at compile time and equal between the two programs. Furthermore, I'd like to automatically make sure at compile time that the map is one-to-one. How would you solve that? (c++0x-extensions are allowed). The first part is easy: Share a template < typename T > stru...

Is constructor use allowed with case classes?

I have a case class (let's name it Stuff) that I want to be able to create anonymous subclasses of at run time by extending a trait (call it Marker). Here's a snippet of a REPL session that illustrates what I'm trying to do: scala> trait Marker defined trait Marker scala> case class Stuff(i: Int) defined class Stuff scala> val a = Stu...

How to deduce class type from method type in C++ templates?

In templates as shown below, I would like the call Run(&Base::foo) succeed without the need to name the Base type twice (as is done in the compiling Run<Base>(&Base::foo) call). Can I have that? Possibly without adding a ton of Boost headers? With the provided code, I get an error of: prog.cpp:26: error: no matching function for call t...

Scala traits / cake pattern vs case classes

In my web application authorized user has at least 4 "facets": http session related data, persistent data, facebook data, runtime business data. I've decided to go with case class composition instead of traits for at least two reasons: traits mixing can cause name clashes i want the free case class goodies like pattern matching and co...

scala and traits on object instances

hi, if i have a trait: trait MyTrait { def doSomething = { println("boo") } } I can add it to a class with "extends": class MyClass extends MyTrait { .... } or i can add it to a new object when i construct it: var o = new MyOtherClass with MyTrait o.doSomething but... can i add it to an existing ob...

real time gui for python using only traits.

Is it possible to create a ui using traits from python to make an interface for a cellular automata simulation? ...

Specifiying a templatized class with traits

I have a struct which indicates a trait: template<typename T> struct FooTraits { static const NEbool s_implementsFoo = false; }; And I can specialize it with a class, thus: class Apple {}; template<> struct FooTraits< Apple > { static const NEbool s_implementsFoo = true; }; However currently I cannot use FooTraits if the c...

Is it possible to automatically coerce parameters passed to delegated methods (from the Array trait) using Moose/MooseX::Declare for Perl?

I'm creating a class which will contain a list of IP addresses, as Net::IP objects. I've wrapped the Net::IP object as a subtype (IPAddress), and defined a coercion from a string to IPAddress. Then I've added an attribute to the class called ip_list with the type ArrayRef[IPAddress], and delegated to the push method of the Array trait....

code reducing by using traits and the with-keyword

Hello, I have some classes with the same super-type. Therefore all of this classes have to override the same methods. Now I can call a method and commit it an object of the common super-type. But it is not always useful to react to each committed type therefore an exception is thrown. First i tried to solve this behaviour like this: de...