News for hackers. Stuff that matters.
Re: Haskell is our first choice for building production soft... by mehrdadn on Monday January 11, 2021 @07:33AM (#25728056)
> You have to repeat the type information, a lot.

Nope you don't, that's what typedefs are for. They're underrated for sure though. People don't use them nearly as much as they should. They're incredibly valuable for avoiding precisely this problem.

Re: Haskell is our first choice for building production soft... by nicoburns on Monday January 11, 2021 @07:38AM (#25728099)
That doesn't stop you having to type e.g.

String foo = "bar"; String baz = foo;

The `String`s can be completely avoided in languages with type inference because it's obvious that a string literal is a string.

Re: Haskell is our first choice for building production soft... by mehrdadn on Monday January 11, 2021 @07:48AM (#25728184)
I thought the complaint was about the logic duplication, not the extra keystrokes. If you want type inference you already have auto. If you want to minimize your keystrokes, you're using the wrong language to begin with, whether there's type inference or not. C++ is designed for writing software robustly, not quickly. (<-- This is not a trivial or obvious statement btw. It took me several years to grasp this. And I viewed C++ from an entirely different perspective when it finally sunk in for me that I would appreciate C++ much more if I decided to make minimizing keystrokes a non-goal.)
Re: Haskell is our first choice for building production soft... by secondcoming on Monday January 11, 2021 @07:56AM (#25728265)
> If you want type inference you already have auto

or auto&&. Did you really intend to make a copy?

Re: Haskell is our first choice for building production soft... by mehrdadn on Monday January 11, 2021 @08:01AM (#25728307)
You probably did intend it to be a copy if you're binding it to a variable and need it to be non-const (like in the example)!
Re: Haskell is our first choice for building production soft... by secondcoming on Monday January 11, 2021 @08:18AM (#25728483)
Ah, but you knew that because that's how the code was written!

If it was instead

    String foo = "bar";
    auto baz = foo;
you don't know for sure. But the code compiles so it obviously ok!
Re: Haskell is our first choice for building production soft... by mehrdadn on Monday January 11, 2021 @08:22AM (#25728521)
No, I'm saying even in that case you know it was intended to be a copy. If you wanted that to be a reference then you'd either (a) just do the obvious thing which is to just use the original variable name instead of creating a new variable out of the blue for no reason, (b) leave a comment explaining why you're not doing the aforementioned obvious thing, or (c) use a self-explanatory variable name to provide the explanation instead of a comment.
Re: Haskell is our first choice for building production soft... by UweSchmidt on Monday January 11, 2021 @07:54AM (#25728241)
Is it really that painful to write "String" each time? You spend at least a fraction of a second anyway to verify that you're writing the right thing, to reconsider if you should use an object or constant or refactor the function to work with a Boolean instead of a naked string; why is writing out the type such a big deal everytime this topic comes up?

I remember my first attempts at programming and being annoyed that I can't add a string and an int; ever since that little bit of housekeeping of using types made sense to me and I can clearly see how it eliminates entire classes of errors.

Re: Haskell is our first choice for building production soft... by wtetzner on Monday January 11, 2021 @11:27AM (#25731172)
> Is it really that painful to write "String" each time?

I find it more painful to read code that has too many type annotations. I also find it painful to read code that has too few, so I'd argue there's a bit of an art to it.

But languages that have type inference but allow type annotations at least allow you to try to hit that balance.

Re: Haskell is our first choice for building production soft... by leshow on Monday January 11, 2021 @12:11PM (#25731980)
> I remember my first attempts at programming and being annoyed that I can't add a string and an int; ever since that little bit of housekeeping of using types made sense to me and I can clearly see how it eliminates entire classes of errors.

Type inference doesn't make these errors go away.

And about your other point, it's unfair to look at just a simple case of writing "string" or not as the only thing inference provides. Although I'd argue that leaving out types where possible helps readability-- it's really the more elaborate cases or intermediate steps during a longer transformation that inference helps with. Not to mention the fact that inference in closures is also really nice.

Re: Haskell is our first choice for building production soft... by silluk on Monday January 11, 2021 @11:25AM (#25731130)
To me it's not the trivial cases like this that make type inference useful. It's when you get longer types like `Arc<Mutex<HashMap<String, String>>>`. Granted, that could be solved with a `type` declaration (or `typedef` in C++) but it's still convenient to be able to say: `let mut x = Arc::new(Mutex::new(HashMap::new()));` and let the compiler figure out the rest based on usage.
Re: Haskell is our first choice for building production soft... by oblio on Monday January 11, 2021 @07:45AM (#25728159)
Java now has (limited) type inference, bar. As does C++, auto. They're limited but they remove a lot of tedium.
Re: Haskell is our first choice for building production soft... by secondcoming on Monday January 11, 2021 @07:55AM (#25728255)
And it's my experience that that is only a benefit to the person who wrote the code, and only for a short time.

Generally, I prefer being able to read a line of code and understanding exactly what it does. If I need an IDE and have to repeatedly try to find the definition of something then, in my opinion, that's wasting my time.

C++'s 'auto' is really useful but it's over-used IMO. I think that there's a belief that if you're not using 'auto' everywhere then you're not writing 'modern' C++. Just becuase your code compiles doesn't necessarily mean it's correct.

Re: Haskell is our first choice for building production soft... by mehrdadn on Monday January 11, 2021 @08:18AM (#25728488)
+1 for auto being overused. I always felt I was shouting into the void (hah) by saying the same thing... it's nice to see someone else agrees.
Re: Haskell is our first choice for building production soft... by nicoburns on Monday January 11, 2021 @11:33AM (#25731277)
I find that types can reduce readability as well as enhance it. They add noise and make it harder to concentrate on the variable names which are often much more important than the types which are often (but certainly not always) obvious from context.
Re: Haskell is our first choice for building production soft... by secondcoming on Monday January 11, 2021 @03:26PM (#25736091)
Interesting point.

I'd never have believed it myself, but find myself using acronyms instead of variable names when the type allows it.

    void foo(MyType mt, const MyOtherType& mot);
It's the variable names that are the noise, types are everything. And no, it's not Hungarian notation either in case anyone suggests it!

However, it maybe doesn't work that well with things like class member names. YMMV

Re: Haskell is our first choice for building production soft... by nicoburns on Monday January 11, 2021 @05:22PM (#25738325)
I guess it partly depends on how varied your types are. In some domains you can find yourself working with 10 variables that are all strings, or all floats/integers. At that point the type isn't that helpful for distinguishing which variable is which.
Re: Haskell is our first choice for building production soft... by secondcoming on Tuesday January 12, 2021 @05:58AM (#25745162)
I should have added that fairly liberal use of type aliases also helps.

    using B64String = std::string;

    B64String encode(const std::string& text);
    std::string decode(const B64String b64);
Re: Haskell is our first choice for building production soft... by corty on Monday January 11, 2021 @08:57AM (#25728886)
Code that specifies types instead of using auto is, barring compiler bugs, usually less correct. The compiler knows better than you what the type really is.
Re: Haskell is our first choice for building production soft... by msla on Monday January 11, 2021 @08:00AM (#25728299)
Shifting topics a bit, typedefs don't allow me to write generic code. Templates do, but templates bring in their own problems, in addition to not being expressive in the right ways: I can have an array of T, but I can't specify that T is Numeric?

The fact C++ doesn't have Numeric but instead has int and long and unsigned and long long and float and double all off on their own is another problem: The compiler knows enough about them to have complex promotion rules but doesn't know enough to allow me to refer to all of them under one name in my code.

Re: Haskell is our first choice for building production soft... by mehrdadn on Monday January 11, 2021 @08:12AM (#25728414)
You're asking for the impossible. What you want is precisely what templates are, but you also want them to not be "templates" for... some bizarre reason.

> I can have an array of T, but I can't specify that T is Numeric?

Sure you can. If you have C++20 concepts:

  template<class T>
  concept Numeric = std::integral<T> || std::floating_point<T>;
  
  template<Numeric T>
  T twice(T x)
  { return x + x; }
Or if you're on a C++11 compiler:

  template<class T>
  typename std::enable_if<
    std::is_arithmetic<T>::value,
    T>::type twice(T x)
  { return x + x; }
Re: Haskell is our first choice for building production soft... by jcelerier on Monday January 11, 2021 @08:49AM (#25728802)
the C++ 20 version can be simplified a bit to:

    Numeric auto twice(Numeric auto x)
    { return x + x; }
Re: Haskell is our first choice for building production soft... by mehrdadn on Monday January 11, 2021 @03:20PM (#25735949)
Probably not a good idea since the caller won't know what the return type is at that point, and the return type would become dependent on the implementation, which breaks function abstraction.

And imagine what would happen when you get a few more 'auto' variables in the return expression. Suddenly your return type will depend on the implementation of your callees. And the code can then quickly become impossible to understand.

auto is overused.

Re: Haskell is our first choice for building production soft... by jcelerier on Monday January 11, 2021 @04:31PM (#25737455)
> Suddenly your return type will depend on the implementation of your callees

Why would that be a problem ? It's super common in templates and has never troubled me the least

Re: Haskell is our first choice for building production soft... by mehrdadn on Monday January 11, 2021 @04:57PM (#25737913)
It might be common practice but it shouldn't be. There are lots of reasons this is a bad idea; here's just a sampling:

1. "My return type is whatever I happen to return" circumvents the ability of the type checker to ensure correctness.

2. More generally, the purpose of a specification (a function declaration in this case) is to declare what is required of a compliant implementation, and to provide a way to check the validity of that implementation. But when you make the types all become auto-deduced, you're basically reducing the specification to a ~ shoulder shrug "it does whatever it does" ~.

3. Moreover, as I alluded to in the comment, it quickly becomes near-impossible to meaningfully separate the definition from the declaration, whether that's because you want to hide it or because you want to compile it separately. Simply put, you lose modularity. It seems like a minor thing when (as in the example) the return value doesn't depend on types inferred from other callees' return values, but as soon as that ceases to be true, you suddenly tie together the implementations of multiple functions. At that point, your functions lose much of their power to abstract away anything, since as soon as you change the return expression for one function, it has the potential to break code (up to and including causing compilation errors) in the the entire chain of callers. (!)

4. Templates end up getting re-instantiated far more often than they need to be (which can slow down both the compilation and the runtime efficiency). You almost certainly don't want '0' and '(size_t)0' to result in duplicate instantiations when dealing with sizes, for instance.

5. Issue #4 can also result warnings/errors/bugs, since now you have a function that returns a different concrete type than you likely intended, which can result in everything from spurious warnings (signed/unsigned casts, for instance) to actual bugs (later truncation of other variables whose types were inferred incorrectly as a result).

6. The code becomes difficult for a human to read too. You now no longer have any idea what types some variables are supposed to be. Not only does this hamper your ability to cross-check the correctness of the implementation itself (just as with the declarations, in #1) but unless your function is trivial, this quickly makes it harder to even understand what the code is doing in the first place, never mind what it's supposed to do.

7. Proxy types become impossible to implement, since they won't undergo the intended conversions anymore.

All this just to reduce keystrokes might be a common trade-off, but a poor one. I can come up with more reasons, but hopefully this gets the point across.

Re: Haskell is our first choice for building production soft... by msla on Monday January 11, 2021 @09:56AM (#25729613)
Bah. Leave it to the latest versions of C++ to show me up.
Re: Haskell is our first choice for building production soft... by dkersten on Monday January 11, 2021 @10:32AM (#25730212)
That's the point though, isn't it? To improve on areas that were lacking in older versions. It can be hard to keep up, though!
Re: Haskell is our first choice for building production soft... by mehrdadn on Monday January 11, 2021 @02:05PM (#25734394)
It's not just the latest version though? C++11 could already do what you wanted.
Re: Haskell is our first choice for building production soft... by MaxBarraclough on Monday January 11, 2021 @08:14AM (#25728438)
edit: I see dataflow beat me to it. I'll leave this here anyway.

> I can have an array of T, but I can't specify that T is Numeric?

This is what type-traits and 'concepts' are for, right?

> The compiler knows enough about them to have complex promotion rules but doesn't know enough to allow me to refer to all of them under one name in my code.

This is what std::is_integral gives you.

https://en.cppreference.com/w/cpp/types/is_integral