Advertisement
Guest User

Untitled

a guest
Aug 6th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.06 KB | None | 0 0
  1. package database
  2.  
  3. import scala.reflect.ClassTag
  4.  
  5. trait Mapper[T] {
  6.   def map(row: List[Any]): T
  7. }
  8.  
  9. class TestUnappMacro {
  10.  
  11.   import TestUnappMacro._
  12.  
  13.   def callByName(f: => Unit) = f
  14.   def callAnon(f: () => Any) = f
  15.  
  16.   callAnon(() => {
  17.     case class Something(id: Int, name: String)
  18.     getMapper[Something](this).map(List(10, "jack"))
  19.   }) // так работает
  20.  
  21.   callByName {       // так нет
  22.     case class Something(id: Int, name: String)
  23.     getMapper[Something](this).map(List(10, "jack"))
  24.   }
  25.  
  26. }
  27.  
  28. object TestUnappMacro {
  29.  
  30.   def getMapper[T: ClassTag](out: Any) = {
  31.  
  32.     val tt = implicitly[ClassTag[T]]
  33.  
  34.     val rclass = tt.runtimeClass
  35.  
  36.     val ctor = rclass.getConstructors()(0)
  37.     val ctypes = ctor.getParameterTypes
  38.  
  39.     ctypes.foreach((t) => println(t))
  40.  
  41.     val cl = getClass
  42.  
  43.     new Mapper[T] {
  44.       def map(row: List[Any]): T = {
  45.         ctor.newInstance(out.asInstanceOf[Object]::row.map(_.asInstanceOf[Object]):_*).asInstanceOf[T]
  46.       }
  47.  
  48.     }
  49.   }
  50.  
  51. }
  52.  
  53. object Run extends App {
  54.   new TestUnappMacro
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement