您当前的位置: 首页 >  段智华 ar

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

段智华 发布时间:2015-08-01 11:00:56 ,浏览量:3

 

定义一个隐式转换函数,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
:7: error: not found: type int
       val i:int=3.5
             ^

scala> val i:Int=3.5
: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>

关注
打赏
查看更多评论

段智华

暂无认证

  • 3浏览

    0关注

    1232博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录