4 ms·
How can you have an alpha without generics?
by mopo2000 12y ago
How can you have an alpha without generics?
- steveklabnik 12y agoWe have them.
- Gankro 12y agoLike three different kinds, too! // Old-style generics; monomorphized // with S as an "input" type of MyTrait fn foo<S, T: MyTrait<S>>(elem: T) { ... } // Where-clause-style generics; monomorphized, // with S as an "output" (associated) type of MyTrait fn foo<S, T>(elem: T) where T: MyTrait<Thing = S> { ... } // Trait objects; dynamic dispatch fn foo(elem: Box<MyTrait>) { ... }
- kimundi 12y agoWell, the first two are really just the same thing :P
- Gankro 12y agoEh, with the new oprhan/impl rules associated and uh... not... associated(...?) types have developed quite a schism.
- dbaupp 12y agoWell, the first two confusingly change two variables at once: the things that are the same (which I presume is what kimundi meant) are fn foo<S, T: MyTrait<S>>(elem: T) { ... } fn foo<S, T>(elem: T) where T: MyTrait<S> { ... } and fn foo<S, T: MyTrait<Thing = S>>(elem: T) { ... } fn foo<S, T>(elem: T) where T: MyTrait<Thing = S> { ... }
- Twisol 12y agoI think you misunderstood something somewhere. This alpha definitely has generics. For example: enum Option<T> { Some{value: T}, None }
- nercury 12y agoYou can read up here: http://doc.rust-lang.org/book/generics.html http://doc.rust-lang.org/book/generics.html, as well about "output" generics here: https://github.com/rust-lang/rfcs/blob/master/text/0195-associated-items.md https://github.com/rust-lang/rfcs/blob/master/text/0195-asso..., and explore usage in collections here: http://doc.rust-lang.org/nightly/std/collections/ http://doc.rust-lang.org/nightly/std/collections/.
- 12423gsd 12y agoAre "Traits" basically C++ Concepts?
- steveklabnik 12y agoThere are some technical details that make them different, if I recall correctly. But they're kinda similar. They're also close to Haskell's typeclasses.
- nercury 12y agoFor someone who knows C#, I would explain them as abstract extension methods that can be overriden for specific types.
- adrusi 12y agoSomewhere between C++ concepts and Haskell typeclasses with a hint of Java interfaces. But yes, their primary usage is to require that type parameters exhibit a certain set of properties.
- bjz_ 12y agoThey fill the same role as concepts - ie. bringing type checking to the call site when using parametrized types, rather than using a duck typed approach, which leads to the big template stack traces that you get in C++.
- nercury 12y agoTo be fair, there were _some_ languages and frameworks which did not have generics on the 1.0 release. It is a bit strange to assert that a new language should.
- legulere 12y agoIt's pretty hard to add them afterwards in a backward-compatible way that is not a great hack
- pinumbernumber 12y agoIt does have generics, unless I'm misunderstanding you