trait

What are some good examples of Mixins and or Traits?

I was reading up on Ruby, and learned about its mixins pattern, but couldn't think of many useful mixin functionality (because I'm not used to thinking that way most likely). So I was wondering what would be good examples of useful Mixin functionality? Thanks Edit: A bit of background. I'm Coming from C++, and other Object languages, b...

can't extend two traits that have a method with the same signature?

Why is the error below? How to workaround it? EDIT: 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 Ascala> trait A {def hi ...

Scala semantics of equals/hashCode for case classes with traits

I am a newcomer to Scala. In 2.7.7, the following code abstract class C case class CC() extends C trait T val c1 = CC() val c2 = new CC() with T println(c1.hashCode == c2.hashCode,c1 equals c2) prints (false,true) whereas I would have expected (false,false) What am I missing? Thanks in advance. ...

How do you return an Iterator in Scala?

What must I do in order to be able to return an Iterator from a method/class ? How would one add that trait to a class? ...

Extending a Scala collection

I want a Map that throws on attempt to overwrite a value for existing key. I tried: trait Unoverwriteable[A, B] extends scala.collection.Map[A, B] { case class KeyAlreadyExistsException(e: String) extends Exception(e) abstract override def + [B1 >: B] (kv: (A, B1)): Unoverwriteable[A, B1] = { if (this contains(kv _1)) t...

C++: get const or non-const reference type from trait

I am writing a functor F which takes function of type void (*func)(T) and func's argument arg. template<typename T> void F(void (*func)(T), WhatTypeHere? arg) { func(arg); } Then functor F calls func with arg. I would like F not to copy arg, just to pass it as reference. But then I cannot simply write "void F(void (*func)(T), T&)"...

trait implementation

If I have some traits like: trait A {...} trait B extends A{...} trait C1 extends B{...} trait C2 extends A{...} I can write class in two ways (C1 and C2 add same functionality) class Concrete1 extends B with C1 class Concrete2 extends B with C2 What variant is better(efficient)? ...

Restrictions in trait mixing

I want to have classes that can mix only specified traits: class Peter extends Human with Lawful with Evil class Mag extends Elf with Chaotic with Neutral Is in scala a way to do this? UPD: trait Law trait Lawful extends Law trait LNeutral extends Law trait Chaotic extends Law trait Moral trait Good extends Moral trait Neutral exte...

Algorithm mixing

I have a class that extends Iterator and model an complex algorithm (MyAlgorithm1). Thus, the algorithm can advance step by step through the Next method. class MyAlgorithm1(val c:Set) extends Iterator[Step] { override def next():Step { /* ... */ } /* ... */ } Now I want apply a different algorithm (MyAlgorithm2) in eac...

Scala initialization behaviour

Please look at the following code. trait MyTrait { val myVal : String } class MyClass extends MyTrait { val myVal = "Value" } class MyClass2(val myVal: String) extends MyTrait Why does the initialization order differ in case of MyClass and MyClass2? The constructor of MyClass will be as MyClass() { MyTrait$class.$init$(this); ...