What is the best way to use enrich-my-library in scala?

大城市里の小女人 提交于 2019-12-10 13:13:52

问题


The are two different way to implement it.

One is more short

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

Second is more long

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

Which one is better?


回答1:


The first version uses a structural type. It makes it possible to write short and readable code, but a disadvantage of structural types is that reflection is used at runtime when you call the method in the structural type. Calling a method via reflection is slower than calling a method directly.

More details are in this blog post (written by me): Avoid structural types when pimping libraries



来源:https://stackoverflow.com/questions/9402218/what-is-the-best-way-to-use-enrich-my-library-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!