About arrows

Being a polyglot developer

Thomas Künneth
Tommis Programming Blog
2 min readApr 5, 2020

--

Part of a keyboard with play buttons for media players
Arrows as symbols on media play buttons

Some time ago I gave two talks during our company conference MATHEMA Campus. One introduced jshell (a REPL that is shipped with Java), the other one offered a glipse at the Dart programming language. While demoing jshel I entered a lambda expression. That is… I tried. To be honest, I screwed up the input by using => instead of ->. Now you may be tempted to think That guy doesn’t know his tools, so what?? It might not be so easy. Let me explain…

In Dart => is used for one line functions, as in:

test(var i) => i * i;main() {
print(test(4));
}

text receives one parameter and returns the result of multiplying it by itself (probably should have called it square). The Dart talk was scheduled for the following day, so my brain must have pressed fast forward. Oh, we’ll return to this nice metaphor later. C# uses => for lambda expressions. C, on the other hand, uses -> in conjunction with a pointer to access members of a sctruct or union. And ES6 introduced arrow functions that look like this:

let test = (x,y) => x*y;

Swift also knows ->. It precedes return types. For example:

func square(x: Int) -> Int {
return x * x
}

You can have a single line function style by utilizing a closure:

let square = { (x: Int) -> (Int) in x * x }

That’s nice. Certainly, Kotlin has arrows, too. For example -> separates the…

  • … parameters and body of a lambda expression
  • … parameters and return type declaration in a function type
  • … condition and body of a when expression branch

The Swift example might look like this:

val square = {x: Int -> x * x}

As Kotlin has one line functions out of the box, we can however write:

fun square(x: Int): Int = x * x

So far we have seen two types of arrows, -> and =>. But of course, there’s more to bring joy to the polyglot programmer. In many C-like languages we for example have >> and <<. Yeeeeeesssss, the fast forward and rewind buttons.

Remember, we use them like this…

int a = 2;
System.out.println(a << 3);
System.out.println(a >> 1);

That shall be it for today. Allow me to press the puase button for now. But be sure to expect a piece on ||. :-)

A previous (less complete) version of this article was published 2017–04–30 on my Blogger.com blog Tommis Blog. I deleted the weblog in the wake of the GDPR (General Data Protection Regulation), so the original post is no longer available (or only through the Wayback machine of the Internet Archive).

If not stated otherwise images are © Thomas Künneth

--

--

Thomas Künneth
Tommis Programming Blog

Google Developer Expert for Android. Author. Speaker. Listener. Loves writing.