2013-03-15 36 views
3

为什么下面的代码工作?斯卡拉奇怪的地图功能行为

scala> List(1,2,3) map "somestring" 
res0: List[Char] = List(o, m, e) 

它在2.9和2.10都有效。 展望类型确定:

[master●●] % scala -Xprint:typer -e 'List(1,2,3) map "somestring"'                    ~/home/folone/backend 
[[syntax trees at end of      typer]] // scalacmd2632231162205778968.scala 
package <empty> { 
    object Main extends scala.AnyRef { 
    def <init>(): Main.type = { 
     Main.super.<init>(); 
    () 
    }; 
    def main(argv: Array[String]): Unit = { 
     val args: Array[String] = argv; 
     { 
     final class $anon extends scala.AnyRef { 
      def <init>(): anonymous class $anon = { 
      $anon.super.<init>(); 
      () 
      }; 
      immutable.this.List.apply[Int](1, 2, 3).map[Char, List[Char]](scala.this.Predef.wrapString("somestring"))(immutable.this.List.canBuildFrom[Char]) 
     }; 
     { 
      new $anon(); 
     () 
     } 
     } 
    } 
    } 
} 

看起来它被转换为WrappedString,其中有一个适用的方法。这解释了它是如何工作的,但没有解释WrappedString如何被接受到A => B类型的参数中(如在scaladoc中指定的那样)。有人能解释一下,请问这是怎么发生的?

回答

7

截至collection.Seq[Char]方式,这是PartialFunction[Int, Char]一个亚型,这是Int => Char子类型:

scala> implicitly[collection.immutable.WrappedString <:< (Int => Char)] 
res0: <:<[scala.collection.immutable.WrappedString,Int => Char] = <function1> 

所以,只有一个隐式转换发生的事情,原来String => WrappedString,该踢,因为我们正在处理一个像函数一样的字符串。

+0

好,我知道了。谢谢。 – folone 2013-03-15 13:30:15

4

由于WrappedString具有作为超类型(Int) => Char

Scaladoc和展开 '线性超型' 部分。

+0

我没注意到。谢谢。 – folone 2013-03-15 13:31:05

2

其他人已经明确表示你的String实现了一个接受Int并返回一个char(即Int => Char符号)的函数。这让这样的代码:

scala> "Word".apply(3) 
res3: Char = d 

扩大你的例子更清楚,也许:

List(1,2,3).map(index => "somestring".apply(index)) 
List(1,2,3).map(index => "somestring"(index)) //(shorter, Scala doesn't require the apply) 
List(1,2,3).map("somestring"(_)) //(shorter, Scala doesn't require you to name the value passed in to map) 
List(1,2,3).map("somestring") //(shorter, Scala doesn't require you to explicitly pass the argmument if you give it a single-arg function) 
+0

'List(1,2,3)map“somestring”'更短:有时Scala不要求将方法调用和包装参数放在花括号中:-) – 2013-03-15 13:39:43