tags:

views:

114

answers:

2

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
by template i mean generics in c++ style or like in java
lapinferoce
@lapinferoce: I thought so, and my answer stands. template ("generic") and traits (mixin class composition) are two different topics.
VonC
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