Scala - 隱式轉換和隱式引數

Ayning發表於2016-08-10

隱士轉換是Scala提供的一種語法糖

Implicit definitions are those that the compiler is allowed to insert into a program in order to fix any of its type errors.

Sacla隱士轉換的規則

Marking rule: Only definitions marked implicit are available

Scope rule: An inserted implicit conversion must be in scope as a single identifier, or be associated with the source or target type of the conversion

One-at-a-time rule: Only one implicit is inserted

Explicits-first rule: Whenever code type checks as it is written, no implicits are attempted

 

隱士轉換髮生的場景

conversions to an expected type

conversions of the receiver of a selection

implicit parameters

相關文章