Advertisement
Guest User

Untitled

a guest
Jan 10th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.68 KB | None | 0 0
  1. package p06sandbox
  2.  
  3. import breeze.linalg._
  4.  
  5. import p04various.TypeDef._
  6.  
  7. object TypeErasure {
  8.     trait DenseContent[T] {
  9.         def compute(v: DenseVector[T]): String
  10.     }
  11.  
  12.     object DenseContent {
  13.         implicit object _Real extends DenseContent[Real] {
  14.             def compute(v: DenseVector[Real]) = "real"
  15.         }
  16.         implicit object _Int extends DenseContent[Int] {
  17.             def compute(v: DenseVector[Int]) = "int"
  18.         }
  19.         // etc ...
  20.     }
  21.  
  22.   def print2[T : DenseContent](data: DenseVector[T]) = println(
  23.      implicitly[DenseContent[T]].compute(data)
  24.   )
  25.  
  26.   def main = {
  27.     val v0 = DenseVector(1.2, 1.5, 1.6)
  28.     val v1 = DenseVector(3, 4, 5)
  29.     val a = Array(v0, v1)
  30.     a.map(print2)
  31.   }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement