5 ms·
So type checking for thing inside the <> as well as the ()?
by smegel 11y ago
So type checking for thing inside the <> as well as the ()?
- okasaki 11y agoLooks like Haskell's type classes.
- srean 11y agoIt is.
- repsilat 11y agoI think so. I figure C++ has a static type system in most places because it helps the language be fast -- resolving the target of a method call at compile time turns it into a straight function call, and straight function calls can be inlined etc etc. You know how big your objects might be, so you can put them on the stack and you can copy them back and forth. Templates give you a limited kind of polymorphism without sacrificing all of this nice stuff. Compilation of templates is "interpreted", though -- you don't compile a program to quickly instantiate them, you don't JIT them, the data structure just keeps data structures in memory along with its ASTs and it iterates over them, doing its type-checking and everything else "the slow way". I guess C++ compilation looks a lot like Python interpretation, with "No candidate for operator<(const T)&" standing in for AttributeError, and pages of horrible template messages standing in for tracebacks. This analogy raises a question, though -- if Python doesn't need type checking to get nice error messages, why do C++ templates? An alternative to "assert hasattr(a, '__cmp__')" is try-catch. Why not do the same with std::sort? Put a big compile-time try-catch around it, catch your template errors and present a nice message to your users. What benefits would this have over type checking? If anyone wrote their concept specifications too strictly, they might stop your valid program from compiling, and this wouldn't happen with static try-catch. Maybe they thought you needed a random access iterator when all you needed was a forward iterator. What benefits might concepts provide? Same as regular type checking -- if you know that the provided type has to present a given interface, you probably do something crazy like give your compile-time types vtables for the interface, and dramatically speed up compilation times. I don't know that anyone will actually do anything like this, though.
- the8472 11y agotry-catch-throw-warning is only useful for humans. types on the other hand can be used by tools too. better auto-completion, refactoring support, automatic boilerplate "please fill in method body here" construction etc. and there is that axioms thing which builds on concepts to allow the compiler to do more optimizations. again, more tool-friendly.
- KayEss 11y agoMost of what you propose already exists in the form of type traits together with `static_assert`. Originally concepts hold the promise of also writing adapters so that you could write meta-programs that transformed one type into another compatible with some other part of your code, but I think that part won't come until later on now.
- SamReidHughes 11y agoHaving users have to specify the output of template instantiation errors for all their functions would be far worse than just having concepts.
- mcguire 11y agoThat was kind of my impression, from the discussion in Alexander Stepanov and Daniel Rose's From Mathematics to Generic Programming (which is a pretty decent book, by the way). My understanding[1] is that concepts act like a type system for templates and like typeclasses in Haskell and traits in Rust. [1] http://maniagnosis.crsr.net/2015/07/abstracted-algebra-in-rust.html http://maniagnosis.crsr.net/2015/07/abstracted-algebra-in-ru...