I m new in scala and i wonder what's the differences between traits and template ? When should i use template and when should i use traits ? thanks
Asked
Active
Viewed 700 times
1
-
Scala doesn't have "templates". What do you mean by it? – Daniel C. Sobral Apr 27 '10 at 11:58
2 Answers
5
It depends what you mean by template.
If it is (wikipedia) the feature that allow functions and classes to operate with generic types, then template and trait are two orthogonal notions.
Traits can use generic type too (see this SO question for instance), but there are here to offer an alternative to multiple inheritance, by offering mixin class composition with stackable behavior.
-
-
@lapinferoce: I thought so, and my answer stands. template ("generic") and traits (mixin class composition) are two different topics. – VonC Apr 27 '10 at 13:51
1
"By template I mean generics as in C++ or Java" — lapinferoce
In that case, the contrast does not exist. Traits and classes (abstract or concrete) may both be generic. Only objects support neither parametric nor subtype polymorphism.

Randall Schulz
- 26,420
- 4
- 61
- 81