39 points

The big problem with this take is that they seem to assume it is an all or nothing feature.

Personally I love how rust does it, types are inferred inside a body of a function where they matter less and are generally unambiguous (you need to specify them if they are ambiguous) as you often don’t care as much about the exact type you have. But on function parameters are return types they are never inferred as these form the contract to the rest of your program and a unexpected change to one of these can result in your code breaking somewhere else entirely.

Personally I never really use the automatic type annotations in IDEs, they just add noise I rarely care about.

permalink
report
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
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
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
2 points
*

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

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
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
15 points
*

I can’t speak for OCaml, but type inference provides a lot of benefit in Rust. I already have too many keystrokes as it is, and forcing me to be explicit about everything would just add to the stress of using a keyboard.

I agree that types should be explicit at API boundaries. That’s precisely where you want to catch misuse.

As for the point about inference making code harder to read: I suppose that’s true if you spend a lot of time reading code outside of your editor where you also must know what the types are. But that just sounds like a bad workflow in general. Why are you avoiding using a better tool for the job? Modern code review tools like Github even support LSP-like features to solve this problem; and if your language isn’t supported… just pull the feature branch to review it.

permalink
report
reply
2 points

He explicitly states that its not that bad in Rust because all functions must have type annotations. Its only a problem if your functions are huge (they shouldn’t). I think thats the correct way to go. Local variables can be inferred but anything else should be annotated.

Modern code review tools like Github even support LSP-like features to solve this problem; and if your language isn’t supported… just pull the feature branch to review it.

But now your requiring more tools and effort on the reviewer over, just reading the code.

permalink
report
parent
reply
2 points

But now your requiring more tools and effort on the reviewer over, just reading the code.

This should be completely negligible if you are writing code in the same code base.

permalink
report
parent
reply
1 point

I was already assuming I was working on the same codebase. I am not going to stash my work, checkout the branch and wait for the LSP to start up (if it’s working) just to confirm that your types aren’t doing anything weird. I’d rather just have them annotated correctly in the first place and just read the PR and trust the CI.

permalink
report
parent
reply
1 point
*

It’s really weird to me to base any decisions around how much typing you have to do.

Typing is such a small part of programming I really don’t get it.

stress of using a keyboard

Can you elaborate?

Readability and maintainability are core imo.

permalink
report
parent
reply
3 points
*

It’s really weird to me to base any decisions around how much typing you have to do. Typing is such a small part of programming I really don’t get it.

Typing is a huge part of programming. Have you heard of RSI? People invest hundreds (sometimes thousands) of dollars in ergonomic keyboards just to overcome RSI pain. If you’re younger than 30 you might not be impacted by this, but many people who have been typing every day for over a decade are realizing it’s not sustainable without proper ergonomics.

Readability and maintainability are core imo.

I don’t think you sacrifice these by having local type inference. It’s never been an obstacle for me.

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

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