开发者

What are the performance and maintenance considerations of using Scala's structural types?

My co-workers wont let me use them on both开发者_运维百科 grounds. :(


A performance consideration is that the methods in a structural type are called via reflection, which is a lot slower on the JVM than a regular method call. See this answer and comments for detailed info.


I found that when I need structural types, it's almost always because a library I wanted to use isn't designed properly (e.g. missing superclasses in cases like StringBuilder / StringBuffer).

So if you want to use structural types for other reasons, it might be a design problem - which would be more serious than configuration or performance considerations. Scala's type system is powerful, and using structural types is a little bit like "giving up" to get it right with "normal" means. So if you have some code that seems to "require" structural types, try to analyse why it is that way, and maybe ask the community if it can be improved ( https://codereview.stackexchange.com/ seems like a good place for this) if you are stuck.

BTW, everybody tends to overuse a feature he just "grokked", e.g. beginners pattern match like crazy once they get the concept, even if there are better ways (e.g. in case of Option functions like map, flatMap, foreach...).


It also depends in how much you use them, M. Odersky and G. Dubochet have some benchmarks in their report.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜