隐式转换存在的必要性,
在Java Swing中,按钮点击事件的处理,转换为Scala的的写法如下:
val button = new JButton button.addActionListener( new ActionListener { def actionPerformed(event: ActionEvent) { println("pressed!") } } )
这是典型的Java-Style的代码,虽然使用Scala语法去写的,这个代码,真正有用的只有一句,println("pressed")
改写一下:
button.addActionListener((_: ActionEvent) => println("pressed!"))
但是编译失败,原因是addActionListener方法只能接受ActionListener对象作为参数,而不能使用函数常量作为参数,为了消除这种gap,就得定义隐式转换,将函数(确切的说,是将带有ActionEvent类型的参数的函数)转换为ActionListener对象
implicit def function2ActionListener(f: ActionEvent => Unit) = new ActionListener { def actionPerformed(event: ActionEvent) = f(event) //直接赋值 } )
隐式转换的工作流程:
The way this code works is that the compiler first tries to compile it as is, but it sees a type error. Before giving up, it looks for an implicit conversion that can repair the problem. In this case, it finds function2ActionListener. It tries that conversion method, sees that it works, and moves on.
implicit修饰符可以作用变量、function,class和object上
隐式转换的规则:
1. Marking Rule: Only definitions marked implicit are available
2. 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.
3. One-at-a-time Rule: Only one implicit is tried
4. Explicits-First Rule: Whenever code type checks as it is written, no implicits are attempted.
如果编译时没有类型错误,Scala编译器就不会启动隐式转换功能,即使有满足条件的隐式转换存在
相关推荐
赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 包含翻译后的API...
【课程大纲】 第1讲-Spark的前世今生 共12页 第2讲-课程介绍、特色与价值 共13页 第3讲-Scala编程详解:基础语法 共8页 ...第20讲-Scala编程详解:隐式转换与隐式参数 共9页 第21讲-Scala编程详解:Actor入门 共8页
【课程大纲】 第1讲-Spark的前世今生 共12页 第2讲-课程介绍、特色与价值 共13页 第3讲-Scala编程详解:基础语法 共8页 ...第20讲-Scala编程详解:隐式转换与隐式参数 共9页 第21讲-Scala编程详解:Actor入门 共8页
2 scala很多库在设计的时候,不理解原因,包括Option,Collection的很多看似有冗余的地方 3 很多scala的默认写法,不理解 4 多态的具体化,尤其是协变的意义所在 5 各种重载的符号使用 之前读过 programming in...
【课程大纲】 第1讲-Spark的前世今生 共12页 第2讲-课程介绍、特色与价值 共13页 第3讲-Scala编程详解:基础语法 共8页 ...第20讲-Scala编程详解:隐式转换与隐式参数 共9页 第21讲-Scala编程详解:Actor入门 共8页
【课程大纲】 第1讲-Spark的前世今生 共12页 第2讲-课程介绍、特色与价值 共13页 第3讲-Scala编程详解:基础语法 共8页 ...第20讲-Scala编程详解:隐式转换与隐式参数 共9页 第21讲-Scala编程详解:Actor入门 共8页
【课程大纲】 第1讲-Spark的前世今生 共12页 第2讲-课程介绍、特色与价值 共13页 第3讲-Scala编程详解:基础语法 共8页 ...第20讲-Scala编程详解:隐式转换与隐式参数 共9页 第21讲-Scala编程详解:Actor入门 共8页
赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 赠送Maven依赖信息...
【课程大纲】 第1讲-Spark的前世今生 共12页 第2讲-课程介绍、特色与价值 共13页 第3讲-Scala编程详解:基础语法 共8页 ...第20讲-Scala编程详解:隐式转换与隐式参数 共9页 第21讲-Scala编程详解:Actor入门 共8页
【课程大纲】 第1讲-Spark的前世今生 共12页 第2讲-课程介绍、特色与价值 共13页 第3讲-Scala编程详解:基础语法 共8页 ...第20讲-Scala编程详解:隐式转换与隐式参数 共9页 第21讲-Scala编程详解:Actor入门 共8页
在IntelliJ中调试Scala隐式 本文档概述了用于静态分析IntelliJ中的Scala隐式方法的几种补充方法。 JetBrains提供了的,但不包括有关分析富集对象的说明。 注意:这些快捷方式适用于OSX。 查看隐式转换 突出显示...
【课程大纲】 第1讲-Spark的前世今生 共12页 第2讲-课程介绍、特色与价值 共13页 第3讲-Scala编程详解:基础语法 共8页 ...第20讲-Scala编程详解:隐式转换与隐式参数 共9页 第21讲-Scala编程详解:Actor入门 共8页
Scala隐式转换和隐式参数.md
赠送jar包:scala-compiler-2.11.12.jar; 赠送原API文档:scala-compiler-2.11.12-javadoc.jar; 赠送源代码:scala-compiler-2.11.12-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.12.pom; 包含...
赠送jar包:scala-compiler-2.11.8.jar; 赠送原API文档:scala-compiler-2.11.8-javadoc.jar; 赠送源代码:scala-compiler-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.8.pom; 包含翻译后...
赠送jar包:scala-compiler-2.12.7.jar; 赠送原API文档:scala-compiler-2.12.7-javadoc.jar; 赠送源代码:scala-compiler-2.12.7-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.12.7.pom; 包含翻译后...
赠送jar包:scala-compiler-2.11.0.jar; 赠送原API文档:scala-compiler-2.11.0-javadoc.jar; 赠送源代码:scala-compiler-2.11.0-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.0.pom; 包含翻译后...
赠送jar包:scala-xml_2.11-1.0.4.jar; 赠送原API文档:scala-xml_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.4-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.4.pom; 包含翻译后的API...
赠送jar包:scala-reflect-2.12.10.jar; 赠送原API文档:scala-reflect-2.12.10-javadoc.jar; 赠送源代码:scala-reflect-2.12.10-sources.jar; 赠送Maven依赖信息文件:scala-reflect-2.12.10.pom; 包含翻译后...
主要介绍了scala 隐式转换与隐式参数的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧