【问题标题】:What is the best way to use enrich-my-library in scala?在 scala 中使用丰富我的库的最佳方法是什么?
【发布时间】:2012-03-13 05:15:20
【问题描述】:

有两种不同的实现方式。

一个更短

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)

哪个更好?

【问题讨论】:

标签: scala enrich-my-library


【解决方案1】:

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

更多细节在这篇博文中(我写的):Avoid structural types when pimping libraries

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 2011-02-20
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    相关资源
    最近更新 更多