Scala中隐式转换初体验实战及spark中应用

 

定义一个隐式转换函数,dzhdouble2int,将double转换为int,这样编译器搜索到了从double到int的隐式转换,不再报错。

 

隐式转换在spark运用广泛,如implicit def rddtopairrddfunction等

 

C:\Users\admin>scala
Welcome to Scala version 2.10.4 (Java HotSpot(TM) Client VM, Java 1.7.0_13).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val i:int=3.5
<console>:7: error: not found: type int
       val i:int=3.5
             ^

scala> val i:Int=3.5
<console>:7: error: type mismatch;
 found   : Double(3.5)
 required: Int
       val i:Int=3.5
                 ^

scala> implicit def dzhdouble2int (x:Double)=x.toInt
warning: there were 1 feature warning(s); re-run with -feature for details
dzhdouble2int: (x: Double)Int

scala> val i:Int=3.5
i: Int = 3

 
scala>

 

DT大数据微信公众账号:DT_Spark

DT大数据梦工厂scala的所有视频、PPT和代码在百度云盘的链接地址:http://pan.baidu.com/share/home?uk=4013289088&view=share#category/type=0&qq-pf-to=pcqq.group

 

 

你可能感兴趣的:(Scala中隐式转换初体验实战及spark中应用)