20 July 2014
  1. 实际上2.11版本已经不使用Predef.any2ArrowAssoc了。取而代之的是ArrowAssoc。它将任意对象上->的调用隐式转换成ArrowAssoc上的->方法调用,进而转换成Tuple2 的类型。

     implicit final class ArrowAssoc[A](private val self: A) extends AnyVal {
         @inline def -> [B](y: B): Tuple2[A, B] = Tuple2(self, y)
         def [B](y: B): Tuple2[A, B] = ->(y)
     }
    
  2. class PercentPlus(val number: Int) {
         def +%(p: Int): Double = number * (1 + p/100D) 
    }
    
    implicit def int2PercentPlus(number: Int) = new PercentPlus(number)
    
      scala> 120 +% 10
      res0: Double = 132.0
    

在scala2.11版本中可以直接声明隐式类的简化方式来达到同样效果

```scala
implicit class PercentPlus(val number: Int) {
     def +%(p: Int): Double = number * (1 + p/100D) 
}
```
  1. 虽然可以如题中提示那样用一个常规类和一个隐式转换声明。使用隐式类更简洁一些,如下所示

     implicit class FactorialAssoc(private val self: Int) {
       def ! = (1 to self).product
     }
    
     5!
    

use Scala 2.11.1


blog comments powered by Disqus