logoalt Hacker News

pipeline_peak10/12/20245 repliesview on HN

> fn add(x: Int, y: Int) -> Int

Why do language authors insist the majority of programmers want to type this way? Meaningless arrows and redundant colons.

Is it constructive, like it will lead us to think differently? It feels more like a contest in overcomplicating something as innocent as:

int add(int x, int y)


Replies

mcintyre199410/12/2024

I think it’s because generally the type annotations are optional and it’s much easier to parse that version. Typescript uses a colon instead of arrow for the return type so I think that’s just preference though.

In particular if you removed the types from yours it’d be add(x, y) and the parser wouldn’t be able to distinguish that from a function call. I think that’s why the fn keyword is really useful for the parser.

giraffe_lady10/12/2024

I think there's a pretty good case for the arrow being easier to reason about especially with anonymous functions or if currying gets involved. The other way is lacking a "verb" and it becomes harder to keep track of what's going on in some cases.

The arrow is also conventional in ML family languages, which are a venerable branch of programming whose traditions I respect and enjoy. That's not enough reason alone to keep it maybe but it's not nothing either.

The colon thing whatever, I truly just can't bring myself to care about such fine-grained specifics of syntax. It sounds like a rough life honestly.

mkehrt10/14/2024

The answer is this is how theoretical programming languages have done it since the 1920s, borrowing from the mathematical notation of the time. As programming language theory has made more inroads into practical programming languages in the last 10-20 years, newer languages have borrowed the notation.

I'm biased, having been immersed in PL thoery for a while, but I prefer the colon notation. It works better with type inference, for example. Consider declaring a variable in two ways:

  var x: int = 3;
  // Now add type inference
  var x = 3;
Vs

  int x = 3;
  // Now add type inference
  var x = 3; // We've just changed the type to a keyword and that's weird.
But that's just a personal preference.
show 1 reply
lpil10/12/2024

It’s so they are readable left to right, which is typically easier. The difference can be seen most clearly with writing the type of functions as arguments.

show 1 reply