About arrows

Being a polyglot developer

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() {

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




Short posts about programming

Recommended from Medium

5 Questions with Orta Therox

Unity Basics: The Difference between OnCollisionEnter and OnTriggerEnter

Doing a frontend technical interview with ReactJS

Cloud Foundry Advisory Board Call, Apr 2022: The Korifi Project

ESP32 Project #2: Input & Output

How to buy Soldex native token —  SOLX ?

Object Relational Mapping with Python’s SQL Alchemy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thomas Künneth

Thomas Künneth

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

More from Medium

Mobile Platform MQTT implementation using Adapter Design Pattern

Why Is Android Development Such A Meadow Of Fxckeries?

Android Export and Import Data

Azure Communication Services —  Empowering Developers with Telecommunications — Part 1…