[英]What is the best way to use enrich-my-library in scala?
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? 哪一个更好?
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 更多细节在这篇博客文章中(由我编写): 在拉皮条库时避免使用结构类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.