LoginSignup
17
16

More than 5 years have passed since last update.

なぜ implicit conversion を定義するのに import scala.language.implicitConversions が必要なのか

Last updated at Posted at 2014-08-12

implicit conversion を使ったらこんな warning が出たので、理由を調べた。

[warn] .../Main.scala:8: implicit conversion method toMyRichString should be enabled
[warn] by making the implicit value scala.language.implicitConversions visible.
[warn] This can be achieved by adding the import clause 'import scala.language.implicitConversions'
[warn] or by setting the compiler option -language:implicitConversions.
[warn] See the Scala docs for value scala.language.implicitConversions for a discussion
[warn] why the feature should be explicitly enabled.

ググったところ、SIP-18 に理由が書いてあった。

  • implicit conversion の過剰な使用は多くの落とし穴を作ることになる。
  • implicit conversion はとても強力だし、その効果が理解しやすいので、過剰に使われやすい。
  • implicit parameter を使ったほうが implicit conversion を使うよりも多くの場面で良い設計になる。

ということで、「使い過ぎないように」というわりと普通な理由だった。
3番目の implicit parameter を使ったほうが良いっていうのはどういうことだろう…。

17
16
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
16