6 ms·
I agree. C++ has had some form of local type inference when it comes to inferring template types, but in C++11 the auto keyword makes a tremendous improvement.
by kccqzy 6y ago
I agree. C++ has had some form of local type inference when it comes to inferring template types, but in C++11 the auto keyword makes a tremendous improvement.
Even in Haskell where global type inference is supported, people end up putting type signatures on all top-level declarations because doing otherwise makes code less readable and makes type error messages way too confusing. Global type inference means a single type error in a single function can be propagated by the compiler to arbitrarily faraway at the call site, or even several layers deep in a call site. Solving such errors resulting from global type inference simply isn't worth the time.
- secondcoming 6y agoauto can be dangerous in that it can introduce unexpected copies, i.e. auto vs auto&. You could just use auto&& and be correct most of the time (unless you actually want to make a copy). Herb Sutter's Almost Always Auto advice was not good, in my opinion. I still find I actually need to find out the actual type so when I use it it's only typically when the RHS of the assigment mentions the type, static_cast, make_unqiue etc. I find it saves on typing but not so much for readability. There are different rules if you're writing an STL algorithm versus a final concrete program.
- gnulinux 6y agoI've never written Haskell but I'm a Agda programmer (a Haskell dialect with dependent types) and I almost always put all the types and don't rely on type inference. The obvious exception is if something quick like a = b otherwise I'd always do a : Some -> Type -> MoreType a = f b Readability is a reason why. Types are extremely pedagogic tools. I believe when I think about my code, I just think about types. I don't think about computation, just the topology of types. Can I go to Int from X. If not why? This is very different than when I write Python code. So seeing types in code helps me a lot understand what's going on. But the real reason is: errors. It's so much more easier to understand errors when types are explicit.
- kccqzy 6y agoAgda doesn't have HM-style type inference. Furthermore many times the type is the main content of the program, not so much the value.
- gnulinux 6y agoThis is true, I should have mentioned it, although my point still applies to HM style type inference as well.
- cannabis_sam 6y agoTo a point at least, HM type inference is decidable, but type inference in haskell with certain extensions or in agda (at least with certain pragmas) is not. The possibility of a diverging type-level function would require the type inference algorithm to solve the halting problem. (As I understand it at least, been a while since I got into the details)
- bjourne 6y agoPython has optional typing though.
- Groxx 6y agoPython has optional type hinting for optional static analysis tools (e.g. mypy). Types aren't checked at runtime.
- eru 6y agoType inference in C++ (and Go) is still very limited. It only works in the 'forward' direction. In Haskell, OCaml and eg Rust, the compiler can infer the type of a new variable depending on how you are using it later.