On Tue, Oct 23, 2012 at 2:46 PM, Julien Blanc <[email protected]> wrote: > Lucian Branescu a écrit : > > Something like this > > > http://pcwalton.github.com/blog/2012/08/08/a-gentle-introduction-to-traits-in-rust/ > > Very nice introduction. The only question that arises for me (coming from > c++ ground and comparing this to c++ templates) is why trait > implementation is made explicit ? > > Is it a design decision or a current compiler limitation ? I guess the > compiler could not too difficultly be made smart enough to determine from > its actual interface if a type conforms to a trait. Code generation may be > more a problem, though… >
It is actually a design decision, quite similar to how typeclass in Haskell require explicit instantiation whereas Go's interfaces, like C++ templates, do not. Automatic detection is also called duck typing: it if quacks like a duck, then it's a duck. There are two main disadvantages: - functionally, it means that you can use an object for something it has never really been meant for => just because the signatures of some functions match does not mean that their semantics match too - in terms of codegen, this might imply bloat (C++) or runtime overhead (Go) On the other hand, Haskell's approach is quite practical.... as long as one solves the coherence issue. -- Matthieu
_______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
