6 ms·
Can we have a consistent function notation fn name(a: int, b: int) -> void {} This also helps with clean functional type arguments. swift and rust support th
by crudbug 7y ago
Can we have a consistent function notation
fn name(a: int, b: int) -> void {}
This also helps with clean functional type arguments.
swift and rust support this.
A single consistent notation will be helpful for new polyglots.
- 13415 7y agoWhat's the purpose of the stylized ASCII arrow "->" / why is it needed?
- lytedev 7y agoLooks like it denotes return type? Are you implying it can be omitted syntactically because the closing paren implies the return type would follow?
- pvorb 7y agoYou 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.
- leshow 7y agoDepends 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.
- frou_dh 7y agoThe 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.
- leshow 7y agoWhich 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.
- frou_dh 7y agoI 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.
- leshow 7y agoI 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.
- ithkuil 7y ago> 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.
- maxdamantus 7y agoIntuitively, `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_correspondence 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.
- vips7L 7y agoCan we have a consistent function notation void name(int a, int b) {} C, C++, Java, and C# support this. A single consistent notation will be helpful for new polyglots.
- tapirl 7y agoIMHO, "fn add(a: int, b: int): int" is far more consistent than "fn add(a: int, b: int) -> int". At least it is self consistent.
- baszalmstra 7y agoThats precisely the reason its written like that in Mun
- atombender 7y agoHaving -> denote a return variable has the benefit that you the syntax "a -> b" is non-ambiguously a function type. For example, a function that accepts a function as an argument can be written: fn foo(f: int -> string) Or if the argument name isn't needed in this context (e.g. think interface declarations): fn foo(int -> string) If you see ":" as meaning "has type", then using it for a return type isn't necessarily more consistent, because it's not the function that has a type.
- tapirl 7y agoSome reasonable, but I see a new inconsistency here. Shouldn't fn foo(fn (int) -> string) be more consistent?
- atombender 7y agoIt's not inconsistent, just terser. You could say that "fn" declares a function by name; the type of foo is int -> str. In Haskell, this is written: foo :: int -> str Of course, Haskell is based on lambda calculus, so multiple arguments are just a generalization of partial function application: foo :: int -> int -> str (A function taking two int arguments and returning a string, which is indistinguishable from a function that takes a single int and returns a function that takes a single int and returns a string.)
- tapirl 7y agoAh, looks both have their own rationality. ":" is ok for single-value returns. AS3 also uses ":". The Haskell language looks interesting. Maybe I should spend some time to study it.