您的位置:首页 > 其它

Scala - 隐式转换和隐式参数

2016-08-10 12:38 302 查看

隐士转换是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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: