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
+4
A:
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.
VonC
2010-04-27 08:50:15
by template i mean generics in c++ style or like in java
lapinferoce
2010-04-27 13:44:04
@lapinferoce: I thought so, and my answer stands. template ("generic") and traits (mixin class composition) are two different topics.
VonC
2010-04-27 13:51:13
A:
"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
2010-04-27 14:08:53