I talked a little bit last time about how Tangent's lambdas are a little weird compared to C# and other languages. Today, I'm going to go a little more in depth into that, as well as showing some other examples of use. The first is an example that uses the return value of the lambda:
apply (fn(int): int) twice to (x: int) => int {
fn fn x;
}
entrypoint => void {
print apply (x)=>{x*x} twice to 2;
}
Because of the order of operation inference, fn fn x gets parsed out to the right order, passing the result of one operation to the next.
Now for some weirder stuff. Because of how Tangent parses statements, a lambda can have different interpretations depending on what type it resolves to. So when the compiler matches a lambda to a function parameter it needs to make sure that the lambda works for those types. A simple example:
foo(fn(int):void) => void {
fn 42;
}
foo(fn(string):void) => void {
fn "foo";
}
entrypoint => void {
foo (x) => { print x + 2; };
}
Since string + int makes no sense, the int -> void overload is chosen and this prints 44. Weirder yet, Tangent includes the return type in the work. If the lambda can't return the requested type, that interpretation is discarded. Consider a similar example:
foo(fn(int): int) => void {
print "with int";
print fn 42;
}
foo(fn(int): void) => void {
print "with void";
fn 42;
}
entrypoint => void {
foo (x) => {x;};
foo (x) => {print x;};
}
Here the different bodies return different types, leading to dispatching to the different overloads and avoiding ambiguity.
Nifty, and it all worked once I wrote up the tests. Next will be maybe cleaning up a little of the lambda syntax and testing more of the closure functionality.