5 ms·
IMHO, "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.
by tapirl 7y ago
IMHO, "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.
- markslicker 7y agoIn functional programming (the origin of this syntax), add(a: int, b: int): int can be read as a function 'add' when applied to the parameter tuple (a: int, b: int) has type int, I believe Ocaml, SML, actually support this syntax, whereas '->' (arrow) is a function type constructor in functional programming with 'A -> B' denoting a function from A to B. The type of add by itself in a language like Haskell would be: add :: (int, int) -> int
- atombender 7y agoNot quite, in ML languages, a function declared like this: let sum a b = a + b ...has the type: float -> float -> float Same in Haskell. These languages "emulate" multiple arguments through partial function application. OCaml does not use tuples for arguments.
- markslicker 7y agoYou can certaintly have tupled function arguments in SML/Ocaml, in fact that seems to have been the more common style there, whereas Haskell uses curried functions more commonly. As far as the original sugestion, using '->' (arrow) to denote a return type (like Rust) is nonsense in a functional language and kind of butchering the functional inherited syntax IMHO.
- atombender 7y agoI've never heard of OCaml tuples being more common in arguments. If anything, my impression is they're discouraged (in OCaml, not SML) as a replacement for curried arguments. E.g. see [1]. > is nonsense in a functional language SML/OCaml and Haskell all use ->, so I'm not sure what you mean by this? [1] https://stackoverflow.com/questions/10666913/why-prefer-currying-to-tuple-arguments-in-ocaml https://stackoverflow.com/questions/10666913/why-prefer-curr...
- markslicker 7y agoIt is quite common in SML/ML code I've encountered, probably not as common in modern OCaml. BTW, in OCaml let add ((a:int), (b:int)) : int = a + b Works just fine, whereas: let add ((a:int), (b:int)) -> int = a + b is nonsense, because '->' is a type constructer in OCaml (a function from types to types) not part of an ad-hoc syntax for function declarations (like Rust).