6 points

I prefer type inference. It’s extra clutter that can often be abstracted away with good variable and method names. If it quacks the way I need it then that’s one less thing I need to hold context of in my head.

permalink
report
reply
3 points

You should read the article, because it’s pretty much a direct rebuttal with justifications to this exact argument. You’ve really just re-stated what the article disputes.

Which isn’t to say you’re wrong, I’d just be interested in your response to the arguments.

permalink
report
parent
reply
6 points

The article doesn’t make a persuasive case at all. It immediately backs off by acknowledging that 99% of type inference is fine, because it’s really only complaining about function signature inference, which is an extreme case that only a few obscure ML variants like Ocaml and F# support.

It’s like saying all american movies are terrible, and then clarifying that you’ve only seen White Chicks

permalink
report
parent
reply
1 point

I don’t want to infer types from my code. I’d rather infer the code from the types. Types are the spec, they are small and low in expressiveness, code is big and has infinitely more degrees of freedom than types. The bug surface area is smaller with types.

So it makes sense to use the types (simple, terse, constrained) to generate the code (big, unconstrained, longer to write, bug-prone). Inferring types from code is like building a complex machine without plans, and then using an X-ray diffractometer to extract plans from the physical object.

This is the argument.

This comes back to a perennially forgotten/rediscovered fundamental truth about coding: It is much easier to write code than read code

This is immediately followed by the next part that in any sufficiently large organization, you spend more time reading code than writing code.

Put it all together? Fractional second gains in writing that have meaningful expenses when it comes to reading aren’t worth it once you’re operating at any kind of scale.

If you and your buddy are making little hobby projects. If you have a 3 person dev team. If you’re writing your own utility for personal use… I wouldn’t expect these features to become evident at that scale.

Again, it isn’t saying that it’s something intrinsically wrong, it’s just that there is a trade off and if you really think about it, under most professional environments it’s a net negative effect on efficiency.

permalink
report
parent
reply
4 points

My response to the article is that you’re sacrificing gains in language because some people use outdated tools. Code has more context than what is just written. Many times you can’t see things in the code unless you dig in, for example responses from a database or key value store, or literally any external api. Type inference in languages that have bad IDE support leads to a bad experience, hence the author’s views on ocaml. But in a language like Kotlin it’s absolutely wonderful. If needed you can provide context, but otherwise the types are always there, you can view them easily if you’re using a decent IDE, and type inference makes the code much more readable in the long run. I would say that a majority of the time, you do not care about the types in any application. You care about the data flow, so having a type system that protects you from mismatched types is much more important that requiring types to be specified.

permalink
report
parent
reply
1 point

Maybe I’m missing something:

Does type inference provide a practical benefit to you beyond saving you some keystrokes?

What tools do you use for code review? Do you do them in GitHub/gitlab/Bitbucket or are you pulling every code review directly into your IDE? How frequently do you do code reviews?

permalink
report
parent
reply
22 points

You know what really sucks?

When I have a method that returns a Foo and like 300 places it gets called.

And then I change it to return an ever so slightly different Bar

Ah, now I need to go and update the code in 300 places cause the return type changed.

“Can’t you just sed your codebase?”

Maybe, but it could cause some serious unintended dawizard

permalink
report
reply
6 points
*

If it returned a “Foo”, whose structure changes in such a way as to requires changes in all places it was used…

That, sounds to me like a disaster you avoided by being helped (which is the polite way to describe developers not getting away with ignoring lazy and dangerous type conversion bugs) to fix each and every usage of it.

permalink
report
parent
reply
3 points

No, there’s countless ways code could be consuming a Foo or Bar and not care which.

Literally any form of serialization won’t care, for example.

Also you can change from a Foo to a Bar in a non breaking manner, where it’s name changed but the still have the same interface.

permalink
report
parent
reply
1 point
*

We’re talking about type inference, right?

If you have countless examples, I’d be happy to entertain others that have to do with type inference, because 1) serialisation is not related, 2) renaming in APIs is arguably, also not. Though, I cannot remember the last time my IDE wasn’t able to know, and do this for me.

permalink
report
parent
reply
1 point

Ah, now I need to go and audit the code in 300 places cause the return type changed.

Fixed

permalink
report
parent
reply
14 points

The solution to this problem (and many others) is to use an IDE / editor which supports refactoring like that. Which is pretty much every IDE / editor unless you’re using some very obscure language I think.

permalink
report
parent
reply
3 points

anything that supports your language’s language server protocol

permalink
report
parent
reply
0 points

I dont think external tooling should be a factor in deciding your language’s definition.

permalink
report
parent
reply
2 points
*

Yup, that’s what I meant. I really don’t see why anyone wouldn’t use it nowadays.

permalink
report
parent
reply
38 points

I’m just glad that type inference can improve this sort of situation a bit:

ConfigManager configManager = new ConfigManager();

permalink
report
reply
-5 points

Why not just use auto (assuming C++) or var (assuming C#/Java)?

permalink
report
parent
reply
37 points
*

Because that’s type inference. The exact thing the article is arguing against. And that this comment is saying is nice.

permalink
report
parent
reply
13 points

Like many things… it depends.

Type inference is wonderful for prototyping and writing short lived tools - it is an unnecessary expense for mature projects with a large number of developers on it.

At my shop we have a concept of “one off” routes that are written to accomplish a specific task and intended to be run once. A few of these are run repeatedly but with the understanding that these routes are unmaintained and exempt from automated testing requirements (we’ve got a separate bucket for routes that are only rarely invoked but are complex enough and frequently enough used to get test coverage). For stuff like those one off scripts I’ll never block a PR for omitting typing - while I absolutely will in our regular codebase.

permalink
report
reply
1 point

I entirely agree. It all depends on context, preferences, goal and probably other things.

I found the article interesting even though I don’t entirely agree with all of it!

permalink
report
parent
reply
2 points

To me this is an argument for why Go should not add type inference to function/method declarations. Go is like Rust (I guess, I haven’t used Rust) - type inference works for declaring a variable (or const) and generic type parameters but not for type declarations, methods, functions, etc. I was in the “more inference is always better” camp but now I’m thinking Go has the perfect level of inference. Except for function literals/lambdas. I really want go to infer the argument and return types when I’m passing a function literal/lambda to a function.

permalink
report
reply
5 points
*

The thing about Rust’s type inference that seems wild to anyone who hasn’t seen Hindley-Milner/ML style type systems before is that it’s “bidirectional” (in quotes because that’s not a proper type theory term as far as I know). The type of the left-side of an assignment can determine the type (and behavior!) of the right side. For instance, this is ambiguous:

let foo = [("a", 1), ("b", 2)].into_iter().collect();

The expression creates an iterator over the (letter, number) pairs, and collect() stores the elements in a newly created container. But which container type? Here are two valid variants:

let foo: Vec<_> = [("a", 1), ("b", 2)].into_iter().collect();

This creates a vector with items ("a", 1) and ("b", 2).

let foo: HashMap<_, _> = [("a", 1), ("b", 2)].into_iter().collect();

This creates a mapping where "a" and "b" are keys, and 1 and 2 are the corresponding values.

Playground link in case you’d like to mess with this concept: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=76f999f4db600415643b0c58c19c69b7

permalink
report
parent
reply

Programming Languages

!programming_languages@programming.dev

Create post

Hello!

This is the current Lemmy equivalent of https://www.reddit.com/r/ProgrammingLanguages/.

The content and rules are the same here as they are over there. Taken directly from the /r/ProgrammingLanguages overview:

This community is dedicated to the theory, design and implementation of programming languages.

Be nice to each other. Flame wars and rants are not welcomed. Please also put some effort into your post.

This isn’t the right place to ask questions such as “What language should I use for X”, “what language should I learn”, and “what’s your favorite language”. Such questions should be posted in /c/learn_programming or /c/programming.

This is the right place for posts like the following:

  • “Check out this new language I’ve been working on!”
  • “Here’s a blog post on how I implemented static type checking into this compiler”
  • “I want to write a compiler, where do I start?”
  • “How does the Java compiler work? How does it handle forward declarations/imports/targeting multiple platforms/<other tricky feature>?”
  • “How should I test my compiler? How are other compilers and interpreters like gcc, Java, and python tested?”
  • “What are the pros/cons of <language feature>?”
  • “Compare and contrast <language feature> vs. <other feature>”
  • “Confused about the semantics of this language”
  • “Proceedings from PLDI / OOPSLA / ICFP / <other large programming conference>”

See /r/ProgrammingLanguages for specific examples

Related online communities

Community stats

  • 2

    Monthly active users

  • 272

    Posts

  • 327

    Comments