Hacker News new | ask | show | jobs
by 13415 2443 days ago
What's the purpose of the stylized ASCII arrow "->" / why is it needed?
3 comments

Intuitively, `a -> b` is normally interpreted as "from `a`, I can get `b`".

A function of type `Int -> String` allows you to get a `String` from an `Int`.

The proposition `A -> B` (`A` implies `B`) means that `A` being true allows `B` to be true; we can deduce `B` from `A`.

For the link between functions and implication, see: https://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspon...

I don't think it makes sense to say `fn read(s: string) -> int` as in Rust though. If an arrow were used in such a function declaration, it should probably look something more like:

  fn read: (s: string) -> int
As another commenter has indicated, `fn read(s: string): int` states that `read(s)` is of type `int`, which is logical.
The thing about adopting the ASCII Art pseudo arrow is that it bakes in an assumption that the dash character lines up vertically with the point on the angle bracket character. This simply isn't the case in plenty of fonts. So it often looks like crap if you pay attention.
Which fonts does it not look good in? Haskell, Rust, Swift, Ocaml, and I'm sure some others, all use this notation. And I've never seen the '->' look odd in any font I've ever used.
> And I've never seen the '->' look odd in any font I've ever used.

Funny; I'm reading this in a HN android app; I don't know which font it uses but I swear I'm staring at such a misaligned example.

I just went to the Rust homepage and clicked until I found an example of a function. It's being presented in Monaco where the dash is noticeably higher than the centre of the angle bracket.
I haven't used anything other than linux for more than a decade so that probably explains it. Still, I've never heard this complaint before.
Looks like it denotes return type? Are you implying it can be omitted syntactically because the closing paren implies the return type would follow?
Depends on the language but in Haskell, the (->) arrow is a function type constructor (i.e. constructs function at type level) where a -> b constructs a function type from a to b.

This is often chained to produce functions that 'take multiple arguments'. i.e. a -> b -> b. I put that in quotations because in reality all functions take only one argument, this is called currying. It gives functions flexibility in that you can partially apply them at will.

In languages like Rust and Swift, the syntax is (a: A, b: B) -> B. Because these languages are "uncurried", they use the syntax of uncurried functions. Meaning, they cannot be readily partially applied.

You mean like in Go?

    func add(x int, y int) int {
        return x + y
    }
I doubt this is more readable, but everyone has their own taste.