2010-12-13 4 views
1

Hallo Ich bin auf der Suche nach einem schnellen Beispiel für Klassennutzung in Scala, die in 2.7.7 und 2.8 Umgebungen funktioniert.Suchen Sie zum Beispiel Typ Klassenverwendung, die in Scala 2.7.7 und 2.8 Skripte funktioniert

Alle Beispiele, die ich gesehen habe, funktionieren nur in 2.8, aber mir wurde gesagt, dass Typklassen auch in 2.7.7 umsetzbar sind.

Die einzige andere Voraussetzung ist, dass die Verwendung in der Lage sein muss, in einem Skript zu arbeiten.

Alle Beispiele geschätzt! Dank

Antwort

0

Ich ging mit einer Lösung in diese Richtung:

//---------- Classes without needed behavior 
case class Point(x: Double, y: Double) 
trait Shape {} 
case class Circle(center: Point, radius: Double) extends Shape {} 
case class Rectangle(lowerLeft: Point, upperRight: Point) extends Shape {} 


val circle = Circle(Point(1.0, 2.0), 5.0) 
val rectangle = Rectangle(Point(-2.0, -1.0), Point(4.0, 5.0)) 


//----------- Support for new behavior I want to add 

// Create exception to handle non supported subclasses 
case class NewMethodNotSupported(a: Any) 
    extends RuntimeException(a.toString + ".newMethod() not supported") 

class NewBehavior(shape: Shape) { 
    def newMethod() = shape match { 
     case c: Circle => doMethod(c) 
     case r: Rectangle => doMethod(r) 
     case _ => throw NewMethodNotSupported(shape) 
    } 
    private 
    def doMethod(s: Shape) = println(s) // print to standard out. 
} 

object NewBehavior { 
    // invoked by the compiler 
    implicit def shapeToNewBehavior(s: Shape) = new NewBehavior(s) 
} 

// bring the implicit method in scope 
import NewBehavior._ 
// --------- End of new behavior support   


// Test behavior support: 
println("Test that new behavior is added:") 
circle.newMethod() 
rectangle.newMethod() 

// Create a non supported shape class 
case class Triangle(vertex1: Point, 
     vertex2: Point, vertex3: Point) extends Shape {} 

val triangle = Triangle(Point(-1.0,0.0), Point(1.0,0.0), Point(0.0,1.0)) 

// Catch exception thrown by trying to call method from unsupported shape 
try{  
    println("\nTest method call from unsupported shape:") 
    triangle.newMethod() 
} catch { 
    case dns: NewMethodNotSupported => println(dns); System.exit(-1) 
    case unknown => println("Uknown exception " + unknown); System.exit(-1) 
} 
+0

Dies entspricht nicht dem Typklassenmuster. –

2

Ich nehme an, Sie so etwas tun könnten:

def max[A](list: List[A])(implicit ord: Ordering[A]): A = { 
    list.tail.foldLeft(list.head) ((a, b) => if (ord.lt(a, b)) b else a) 
} 

implicit def toOrdering[A <% Ordered[A]]: Ordering[A] = new Ordering[A] { 
    def compare(a: A, b: A): Int = (a < b, b < a) match { 
     case (true, _) => -1 
     case (_, true) => 1 
     case _ => 0 
    } 
} 

println(max(List(1, 2, 3, 2, 1))) 

Der Code läuft in beiden Scala 2.7.7 und 2.8.0 (in beide getestet jetzt), obwohl die implizite Definition nicht erforderlich ist (und möglicherweise in einigen Situationen schädlich) in Scala 2.8.

+0

Ich bin zur Zeit nicht, dass mit Blick Bounds vertraut, so konnte ich nicht herausfinden, wie Sie Ihre Lösung arbeiten, aber ich herausgefunden, wie es zu tun ein anderer Weg. – klactose

Verwandte Themen