Advertisement
Guest User

Untitled

a guest
Jun 11th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.72 KB | None | 0 0
  1. class Point[T : Numeric](val x:T, val y:T) {
  2.   override def toString = "Numeric(" + x + ", " + y + ")"
  3.  
  4.   def + (other : Point[T]) : Point[T]  = new Point[T] ( plus(this.x, other.x), plus(this.y, other.y))
  5.   def - (other : Point[T]) : Point[T] = new Point[T] ( minus(this.x, other.x), minus(this.y, other.y))
  6.   def unary_- : Point[T] = new Point[T](negate(this.x), negate(this.y))
  7.  
  8.   def == (other : Point[T]) : Boolean = (this.x == other.x && this.y == other.y)
  9.   def != (other : Point[T]) : Boolean = !(this == other)
  10.  
  11.   private val numeric = implicitly[Numeric[T]]
  12.  
  13.   private def plus(x: T, y: T) =  numeric.plus (x, y)
  14.   private def minus(x: T, y: T) = numeric.minus (x, y)
  15.   private def negate(x: T) = numeric.negate (x)
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement