“A Scala Code Trick for Converting Type-Method to Curried Function”
fujohnwang
2012-04-06
scala> case class A(x:Int){ def f(y:Int)=x*y}
defined class A
scala> val af = A(2).f _
af: (Int) => Int = <function1>
scala> af(3)
res0: Int = 6
scala> val af = (_: A).f _
af: (A) => (Int) => Int = <function1>
scala> af(A(2))(3)
res4: Int = 6
TWO Points to pay attention to
- (_:A) // any instance of Type A?!
- .f _ // convert method to function, this I knew before ^_^
I Think This trick should be usedsparingly~
开天窗,拉认知,订阅「福报」,即刻拥有自己的全模态人工智能。