Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.86 KB | None | 0 0
  1. package com.test
  2.  
  3. import scala.reflect._
  4.  
  5. class Test()(implicit m: Manifest[Int]) {
  6.   private val BooleanCls = classOf[Boolean]
  7.   private val IntCls = classOf[Int]
  8.   private val LongCls = classOf[Long]
  9.   private val FloatCls = classOf[Float]
  10.   private val DoubleCls = classOf[Double]
  11.   private val StringCls = classOf[String]
  12.  
  13.   def maybeSet[T: ClassTag](setter: T => Any, value: String, name: String) {
  14.     val v = value + name
  15.  
  16.     val t = classTag[T].runtimeClass match {
  17.       case BooleanCls => v.toBoolean
  18.       case IntCls => v.toInt
  19.       case LongCls => v.toLong
  20.       case FloatCls => v.toFloat
  21.       case DoubleCls => v.toDouble
  22.       case StringCls => v
  23.     }
  24.     setter(t.asInstanceOf[T])
  25.   }
  26. }
  27.  
  28.  
  29. object Main extends App {
  30.   new Test().maybeSet[Int](setBlah, "123", "000")
  31.  
  32.   def setBlah(i: Int): Unit = {
  33.     println("blah")
  34.   }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement