Re: Type Inference is Good
Posted: Oct 2, 2006 3:32 AM
> Now that I am
> developing in C#, I find it easier for some reason
> (perhaps less fear of writing in an OOP style ... the C++
> community frowns on OO style code for some reason?!).
I think it would be more correct to say that they frown at _inappropriate_/unnecessary use of OO, such as using a member function for something that might be better done with a free function.
Take std::string, as an exmaple: Its design is widely derided, even in the community, itself, for its bloated interface. As Kevlin Henney said it: "If std::string looks like it was designed by a committee, it's because it was." :)
Instead, you might have a few core functions - those that are needed to maintain invariants, and leaving the rest (like searching, sorting, etc.) as free functions. There's a very good article about this here: "The C++ Style Sweet Spot" (http://www.artima.com/intv/goldilocks.html)
Unfortunately, mixing member functions and non-member functions lead to two different calling conventions, in C++, but it doesn't have to be like that.
Java programmers typically overuse OO, when going to C++ (I did it as well, having done Java for a while, and coming back to C++ several years ago), as there's for example no free functions in Java. The "static import" stuff that has been added more recently might be used to work around that, though.
> This is in fact only the tip of the iceberg. Cat types can
> quickly get quite very sophisticated because it supports
> polymorphism (the C++ kind, not the ML kind ... what is it
> called universal quantification or something?).
I'm not sure what you're thinking of, and was it the C++ kind or the ML kind you were looking for the name of? Also, from the cat examples, I'm not sure which corresponding C++ polymorphism you were thinking of (if it wasn't ML), but anyway, one term for the inheritance-and-virtual-functions polymorphism is "inclusion polymorphism" (because the type used has to be included in the hierarchy), whereas templates/generics go by the name of "parametric polymorphism"/"unbounded polymorphism".
I guess you might be thinking of templates, as they essentially model the mathematical expression "for all T..." (http://en.wikipedia.org/wiki/Universal_quantification)
Concepts/type classes would then be a way to say "For all T where..." :)
I don't have much experience with type inference, myself (note that C++ will get more of this in the next version of the standard, with "auto"/"decltype"), but I'd like to become more familiar with Haskell/ML.
> define if : (bool (A)->(B) (A:any*)->(B:any*))->(B)
> Here is what this means in English: the if operation
> requires two functions of the same type on the top of the
> stack, and below that a boolean value. If the value is
> false, then the first function popped off the stack is
> executed, otherwise the other one is executed.
Interesting. It looks like a dependent type system. :)