Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

My co-workers wont let me use them on both grounds. :(

like image 812
Landon Kuhn Avatar asked Apr 07 '11 22:04

Landon Kuhn


People also ask

What is structural type in Scala?

Structural typing as defined by Wikipedia “A structural type system (or property-based type system) is a major class of type system, in which type compatibility and equivalence are determined by the type's structure, and not by other characteristics such as its name or place of declaration “ Structural types in Scala ...

Does Scala have duck typing?

Gettings Our Ducks in a Row Fortunately, Scala allows us to write idiomatic code to achieve the same results as duck typing.


3 Answers

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.

like image 178
Jesper Avatar answered Oct 06 '22 03:10

Jesper


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...).

like image 22
Landei Avatar answered Oct 06 '22 02:10

Landei


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

like image 37
Carlos López-Camey Avatar answered Oct 06 '22 04:10

Carlos López-Camey