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.
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.
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. fn foo(fn (int) -> string)
be more consistent?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.)The Haskell language looks interesting. Maybe I should spend some time to study it.
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
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.
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.
> 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-curr...
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).The Rust syntax seems to emulate functional programming, but inconsistently since -> is not used purely as a type constructor or term constructor something like a mix of both in the defintion of functions, also it doesn't seem to support currying.
void name(int a, int b) {}
C, C++, Java, and C# support this.
A single consistent notation will be helpful for new polyglots.
This way it would be consistent with how variable types are declared.
func add(x int, y int) int {
return x + y
}
I doubt this is more readable, but everyone has their own taste.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.
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.
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.