久しぶりにDwangoさんのScala研修テキストを読んでいたら、今まで理解していなかったオプションが出てきたので調べてみた。
dwango.github.io
そのオプションとはこれ。
import scala.language.higherKinds
scala.language.higherKinds
について、Scaladocにはこう書かれている。
Only where this flag is enabled, higher-kinded types can be written.
Why keep the feature? Higher-kinded types enable the definition of very general abstractions such as functor, monad, or arrow. A significant set of advanced libraries relies on them. Higher-kinded types are also at the core of the scala-virtualized effort to produce high-performance parallel DSLs through staging.
そして、対象となるコードはこれ。
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
scala.language.higherKinds
を有効にしないでコンパイルすると以下のような警告が出る。
scala> trait Functor[F[_]] {
| def map[A, B](fa: F[A])(f: A => B): F[B]
| }
<console>:11: warning: higher-kinded type should be enabled
by making the implicit value scala.language.higherKinds visible.
This can be achieved by adding the import clause 'import scala.language.higherKinds'
or by setting the compiler option -language:higherKinds.
See the Scaladoc for value scala.language.higherKinds for a discussion
why the feature should be explicitly enabled.
trait Functor[F[_]] {
^
defined trait Functor
'higher-kinded type'は日本語に訳す(?)と、「高カインド型」という…訳になっていない気もするけど。
高カインド型については、下記のサイトの解説を参考にした。
eed3si9n.com
高カインド型は型コンストラクタを受け取る型コンストラクタだ
型クラスを型パラメータを受け取るようなクラス(ListとかOptionとか)に対して実現するための仕組み、とのことだった。
なぜこれがオプションによってコントロールされるかは、以下のようにScaladocの続きに書かれている。
Why control it? Higher kinded types in Scala lead to a Turing-complete type system, where compiler termination is no longer guaranteed. ....So an explicit enabling also serves as a warning that code involving higher-kinded types might have to be slightly revised in the future.
コンパイラの完了を保証しないし、将来変わる可能性も有るから明示的に有効にしないと警告を出す、ということだそうだ。
一つ勉強になった。