加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在scala中使用enrich-my-library的最佳方法是什么?

发布时间:2020-12-16 18:53:02 所属栏目:安全 来源:网络整理
导读:这是实现它的两种不同方式. 一个更短 implicit def toR1(s:String) = new { def getLength = s.length)} 第二是更长 class R2(s:String){def getLength2 = s.length)}implicit def toR2(s:String) = new R2(s) 哪一个更好? 解决方法 第一个版本使用 structu
这是实现它的两种不同方式.

一个更短

implicit def toR1(s:String) = new { def getLength = s.length)}

第二是更长

class R2(s:String){def getLength2 = s.length)}
implicit def toR2(s:String) = new R2(s)

哪一个更好?

解决方法

第一个版本使用 structural type.它可以编写简短且可读的代码,但结构类型的缺点是在结构类型中调用方法时在运行时使用反射.通过反射调用方法比直接调用方法要慢.

更多详情请见这篇博客文章(由我撰写):Avoid structural types when pimping libraries

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读