2011-01-28 17 views
10

Voglio creare un tratto che aggiunge alcune proprietà a una classe e consente di concatenare i metodi. Testato in Scala 2.8.1.Best practice per implementare il tratto Scala che supporta il metodo di concatenamento

trait SomeProperty { 
    var prop : String = "default" 
    def setProp(s: String) = { 
     prop = s 
     this 
    } 
} 
sealed abstract class Value 
case class IntegerValue(v: Int) extends Value 
case class FloatValue(v: Float) extends Value with SomeProperty { 
    def foo() = { println("I'm foo.") } 
} 
case object UnknownValue extends Value with SomeProperty { 
    def bar() = { println("I'm bar.") } 
} 

scala> val x = UnknownValue 
scala> x.setProp("test").bar() 
<console>:10: error: value bar is not a member of SomeProperty 
    x.setProp("test").bar() 

Qual è la pratica più comune in questo tipo di situazione? (Si preferisce la modalità sicura)

risposta

20

È possibile specificare esplicitamente il tipo di istanza come tipo di ritorno per setProp.

trait SomeProperty { 
    var prop : String = "default" 
    def setProp(s: String):this.type = { 
     prop = s 
     this 
    } 
} 
+0

Questo è meglio. – lscoughlin

+0

Funziona! Ma non capisco perché. Immagino che 'this.type' sia uguale a' SomeProperty', no? Scala specifica? O anche possibile in Java? – ihji

+0

Questo articolo è utile. http://scalada.blogspot.com/2008/02/thistype-for-chaining-method-calls.html – ihji

0

La cosa più semplice da fare è utilizzare un generico.

object Value { 

    trait SomeProperty[X] { 
    var str: String = null; 
    def setStr(s: String): X = { 
     str = s; 
     return this.asInstanceOf[X] 
    } 
    } 

    abstract sealed class Value 
    case class IntegerValue(i: Int) 
    case class StringValue(s: String) extends SomeProperty[StringValue] { 
    def foo(): Unit = { 
     println("Foo.") 
    } 
    } 
    case class UnknownValue(o: Any) extends SomeProperty[UnknownValue] { 
    def bar(): Unit = { 
     println("Bar.") 
    } 
    } 

    def main(args: Array[String]): Unit = { 

    new UnknownValue(18).setStr("blah blah blah").bar 
    new StringValue("A").setStr("halb halb halb").foo 
    } 
} 
1

Non so se questo è quello che stai cercando

scala> trait Property[T] { 
    | me: T => 
    | var prop:String="" 
    | def setProp(s:String) = { 
    |  prop=s 
    |  me 
    | } 
    | } 
defined trait Property 

scala> class A extends Property[A] 
defined class A 

scala> class B extends Property[B] 
defined class B 

scala> val a= new A 
a: A = [email protected] 

scala> val b = new B 
b: B = [email protected] 

scala> a.setProp("Hi") 
res13: Property[A] with A = [email protected] 

scala> a.setProp("Hi").setProp("Bye") 
res14: Property[A] with A = [email protected] 

scala> b.setProp("D") 
res15: Property[B] with B = [email protected] 
Problemi correlati