Live data from Hacker News

Type Inference Was a Mistake

borretti.me

11–20 of 133 posts

Re: Type Inference Was a Mistake

#12
> It makes code harder to read

One could argue it's better for type inference to not just be part of the language but also part of the IDE. E.g. you type

auto x = ...

And then the IDE offers to replace the auto with vector

That way you can both write code with type inference, but read code with fully annotated types!

Re: Type Inference Was a Mistake

#13
post #3
post #2

Sometimes you'll read something and think "I'm not taking the bait".

It's not even factual. > In Rust and Haskell you have to at least annotate the parameter types and return type of functions. Type inference is only for variable bindings inside the function body. This is false for Haskell. Can't speak for Rust.

In rust, you are required to annotate at least the interface. In Haskell, if I remember it well, it is allowed but not required.

Even in Rust, I tend to specify types at variable bindings if the type gets overly complex, just to push errors closer to their cause.

A nice feature of Rust is you can specify partial types, wih underscores for the still-to-infer part. E.g. let x:Vec=someexpression; is a vector of something, but you don't know what exactly.

Re: Type Inference Was a Mistake

#15
post #9

This SOUNDS like “types are bad”. The author’s message (towards the end of the article) is “I don’t want to infer types from my code. I’d rather infer the code from the types. Types are the spec…” Yes. Always annotate types. Keep inference, it tells you when your annotations are inconsistent with tour code.

> Keep inference, it tells you when your annotations are inconsistent with tour code.

Isn't that plain type checking, rather than type inference?

Type checking detects inconsistencies, type inference assigns types in ways that avoid inconsistencies.

Re: Type Inference Was a Mistake

#16
I think the article's take on type inference is a bit heavy-handed and misses some of the nuances of modern software development.

First off, the complaint about reduced readability outside of IDEs feels like a niche problem. Sure, it's a valid point when you're reading code on paper or in a basic text editor, but let's be real: most of us live in IDEs with excellent type hinting capabilities. The argument kind of falls apart when you consider that good variable naming can often make the need for explicit types less critical. Plus, isn't the goal of any good codebase to be as self-documenting as possible?

Regarding OCaml's type inference being a "footgun," it seems like a bit of an exaggeration. Yes, OCaml's system is powerful and can lead to some head-scratching moments, but isn't that just part of the learning curve with any powerful tool? It sounds like the frustration comes more from not leveraging the type system correctly rather than an inherent flaw with type inference. And honestly, adding type annotations for debugging is a pretty standard practice across many languages—not just OCaml.

The point about academic effort being wasted on type inference research also misses the mark. This research pushes the boundaries of what's possible with programming languages, leading to more expressive and safer languages. To frame this as a waste is to ignore the broader benefits of advancing programming language theory. Sure, it'd be nice if papers spent more time on practical applications, but that doesn't mean the theoretical aspects aren't valuable.

It feels like the article is conflating personal gripes with systemic issues. Type inference, when used correctly, can significantly reduce boilerplate and make code more concise and readable. Of course, it's not a silver bullet, and there are situations where explicit type annotations are beneficial for clarity, especially in public APIs. But to dismiss type inference outright seems like throwing the baby out with the bathwater.

In the end, it all boils down to using the right tool for the job and understanding the trade-offs. There's no one-size-fits-all answer in programming, and dismissing type inference entirely overlooks its benefits in many scenarios.

Re: Type Inference Was a Mistake

#18
This is not good. One of the author's arguments is that type inference in an IDE is bad because sometimes I read code in a book where I don't have type inference...

I don't know about other folks, but the vast majority of code I read is in my editor, where the type inference saves me a ton of time and pain.

Re: Type Inference Was a Mistake

#19
post #12

> It makes code harder to read One could argue it's better for type inference to not just be part of the language but also part of the IDE. E.g. you type auto x = ... And then the IDE offers to replace the auto with vector That way you can both write code with type inference, but read code with fully annotated types!

That's actually how I use IntelliJ, I'll type something like

  x = foo();
And then accept the correction it offers to

  vector x = foo();
Which is nice enough since you can invoke all this with just the keyboard.

Re: Type Inference Was a Mistake

#20

I think the article's take on type inference is a bit heavy-handed and misses some of the nuances of modern software development. First off, the complaint about reduced readability outside of IDEs feels like a niche problem. Sure, it's a valid point when you're reading code on paper or in a basic text editor, but let's be real: most of us live in IDEs with excellent type hinting capabilities. The argument kind of fal…

OCaml's type system allows for expressing complex data structures and behaviors in a concise and readable manner. These types can significantly improve code clarity by providing explicit declarations of intent and structure. Here are some examples:

  type 'a binary_tree =
    | Leaf
    | Node of 'a binary_tree * 'a * 'a binary_tree

  type http_response = [
    | `Ok of string
    | `Error of int
    | `Redirect of string
  ]

  module type QUEUE = sig
    type 'a t
    exception Empty
    val empty: unit -> 'a t
    val enqueue: 'a -> 'a t -> 'a t
    val dequeue: 'a t -> 'a option * 'a t
  end

  type _ expr =
    | Int : int -> int expr
    | Bool : bool -> bool expr
    | If : bool expr * 'a expr * 'a expr -> 'a expr

  type person = {
    name: string;
    age: int;
    address: string; 
  }
Post reply on HN