找到你要的答案

Q:missing parameter type for expanded function for primitive to char conversion

Q:用于原始到char转换的扩展函数的缺少参数类型

I know that there are similar question floating on stack-overflow.

But, it will be great, if any simple explanation for below is available some where.

val arr1 = Array.tabulate(5)( (_+65) )

compiles fine.

val arr2 = Array.tabulate(5)( (_+65).toChar )

is a compile time error.

Also, i am wondering how to use -Ytyper-debug as described by @som-snytt in Scala: missing parameter type

我知道有类似的问题漂浮在堆栈溢出。

但是,这将是伟大的,如果任何简单的解释下面是可用的一些地方。

val arr1 = Array.tabulate(5)( (_+65) )

编译好的。

val arr2 = Array.tabulate(5)( (_+65).toChar )

是编译时错误。

Also, i am wondering how to use -Ytyper-debug as described by @som-snytt in Scala: missing parameter type

answer1: 回答1:

when compiler compile:

Array.tabulate(5)( (_+65) )

it will equal to:

Array.tabulate(5)(x => (x + 65 ) )

but for:

Array.tabulate(5)( (_+65).toChar )

it will be expanded to an anonymous function:

Array.tabulate(5)( (x => x + 65).toChar )

the compiler will lose the context of x in function body.

You can use scala -Ytyper-debug to see the details:

the type debug outputs:

((x$1) => x$1.$plus(65)).toChar

编译时编译:

Array.tabulate(5)( (_+65) )

它等于:

Array.tabulate(5)(x => (x + 65 ) )

但对于:

Array.tabulate(5)( (_+65).toChar )

它将扩展为匿名函数:

Array.tabulate(5)( (x => x + 65).toChar )

编译器将失去x在函数体中的上下文。

你可以使用Scala Ytyper调试查看详情:

类型调试输出:

((x$1) => x$1.$plus(65)).toChar
scala  types  parameters