The Dot Operator

Warning: this is an April Fools’ post

Point-free style is a big trend in modern functional programming: it allows to manipulate functions without mentioning their arguments which makes the code concise and compositional. Here is an example from Wikipedia:

This function, written in Haskell, composes a map() function with a filter() function. As you can see, point-free style largely relies on dots.

Kotlin is not a functional language per se, but we are happy to borrow useful features from other languages. So, today I’ll write about dots.

The Dot

We introduce the dot operator in Kotlin. As usual, it works by convention. If there’s a function like this

it will be called whenever there’s a dot (“.”) after an expression of type Foo:

This function prints “a dot!” twice: once for each dot after a Foo. The bar() function doesn’t return a Foo, so the dot after it doesn’t count.

Dot Chaining

What if my dot() function returned something, for example, another Foo?

then, of course, it’s result is used to call whatever comes after the dot:

This prints 2, because the dot() function returned a new Foo with an increased counter.

As you can see, the dot operator gives us a lot of power and addresses most of the issues previously tackled only by aspect-oriented programming.

Flavors of the Dot

The real power of the dot operator comes in when you declare it with a parameter:

The parameter must be of the Dot type, of which there are a few interesting predefined values:

These values correspond, obviously, to the position of the dot:

One might think that in this example dot() is called three times: one for “a.b”, with the argument BOTTOM, then twice for “1..2″: first with LEFT, and then with RIGHT (these are not two BOTTOMs, because “..” is a single token in Kotlin), but in fact it gets called four times, and the fourth time (in fact it comes first) with TOP as an argument.

I’m sure you already see why: in Kotlin, we never forget to dot out i’s, and that dot counts as well; its position is clearly TOP (we could introduce a separate tittle() function, but we found it too inconvenient). Tittles above j’s count just as well, same for dots in ; ! ? etc.

The Depth of the Dot

You may be wondering what happens if dot() is defined like this:

There is a dot after this, which has type Foo, in the body of Foo.dot() function itself, so dot() should be called recursively at that point and, seemingly, never terminate. This is called a higher-order dot, and is executed only if there’s a function dot1() defined on Foo. If dot1(), in turn, contains a higher-order dot, then dot2() is called and so on:

This corresponds to Russel’s approach to set theory called type theory (with its notion of a class), which lies the solid foundation for statically-typed object-oriented programming.

Conclusion

We are planning to support the dot operator very soon. The first version will be restricted to basic ASCII characters such as . : ; ! ? and, of course i, but in the future we plan to expand it to other characters like ё.

Have a nice Dot!

About Andrey Breslav

Andrey is the lead language designer working on Project Kotlin at JetBrains. He also works on making the Java language better, serving as a Java Community Process expert in a group for JSR-335 ("Project Lambda"), and occasionally speak at software conferences such as Devoxx or JavaOne.
This entry was posted in General and tagged . Bookmark the permalink.

20 Responses to The Dot Operator

  1. MuppetGate says:

    :-D

    Nice try!

  2. Ladislav Thon says:

    So the dot covers the “programmable semicolon” case and more, right? And we could make an entire program out of dots, right? This is so awesome! But… can we get a little more pointless?

    • Three dots in a row don’t parse in Kotlin (yet). But thanks for pointing an important case out.

      • Paul says:

        What if i want smth like “.:!?::.” or Kotlin doesn’t parse any symbols with dots if there is more then 2 of them? But in this case I can’t even use cool variables like iii (I prefer not i-j-k in my cycles, but i-ii-iii style…)

  3. “.:!?::.” is not a single token, but a sequence of tokens, .dot() will be called according to the position of a dot in every token.

  4. Ian Clarke says:

    Can we have something similar for whitespace?

  5. Simon says:

    What value of Dot type will be used if I have Spanish-style string in my code
    String s = “¡Viva la Gloria!”
    ?
    The upside-down exclamation mark has the dot which is 100% lower than “i”‘s dot
    ¡i
    It is located practically at the CENTER.
    To my mind predefined values of Dot type are insufficient.
    Probably dot function should take double a and r which are angle and radius of the dot in polar coordinate system.

  6. Simon says:

    Oh, now I think I don’t understand clearly which dots count, and may be dots in string won’t count and my example is not correct
    But the question is still actual.

  7. Alexander says:

    Super!
    I got my first WTF only in the middle of the article =)

  8. Guangyu HE says:

    What’s the meaning of “the fourth time (in fact it comes first) with TOP as an argument.”? Would you have a detail explantation?

  9. Guangyu HE says:

    fun Any.dot(p: Dot) {
    println(“a dot!”)
    }

    compiling the above code , I got the following error:
    Unresolved reference: Dot

    I code with Android Studio 0.5.3, kotlin-plugin: 0.7.271

  10. Stan says:

    To be honest I don’t like it. At least from the first glance. I would say that this dot convention introduces new puzzlers. You can look at code and expect it to do one thing but in fact it will do another.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">