Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Latest commit

 

History

History
99 lines (78 loc) · 3.21 KB

The Great Syntactic Shift.md

File metadata and controls

99 lines (78 loc) · 3.21 KB
title date author tags keywords categories reward reward_title reward_wechat reward_alipay source_url translator translator_url
The Great Syntactic Shift
2012-01-04 01:38:00 -0800
Andrey Breslav
官方动态
false
Have a nice Kotlin!

As the first public preview of Kotlin is approaching (it will be announced on Jan 10th, 2012, which is less than a week from now!), we are putting some things in order… In particular, we have reviewed syntactic forms available in the language, and decided to change a few. These changes are incompatible with the old syntax, and we have migrated all our test data, and will update the publicly available documentation soon. I would like to point out that this is not the last change of this sort. Kotlin is not released yet, and until it is we are constantly gathering feedback and sometimes find out that something needs to be changed. Consequently, there are no backward-compatibility guarantees before the 1.0. We realize how important backward compatibility is, but we’d better be backward compatible to a really good design created according to the needs of real people. Here’s an overview of the changes we’ve made.

The Namespace is dead. Long live the Package. The concept of namespaces evolved into something so close to Java packages, that we decided to rename it. The namespace keyword is replaced by package keyword. Additionally, namespace blocks are no longer supported. The Arrow loses weight An arrow is used in function literals and when expressions. Some languages use a “fat arrow” (=>) and some use a “thin arrow” (->). Initially, we used the fat one, but is has some unfortunate interactions with comparisons, like this:

{% raw %}

{% endraw %}
  val higherThanY  = {x => y <= x}

{% raw %}

{% endraw %}

So we decided to switch to a thin arrow:

{% raw %}

{% endraw %}
  val higherThanY  = {x -> y <= x}

{% raw %}

{% endraw %}

More readable function types In the old syntax we wrote function types as follows:

{% raw %}

{% endraw %}
val f : fun(Int) : String

{% raw %}

{% endraw %}

which is very close to Kotlin’s function declaration syntax, and seems perfectly logical. Unfortunately, as this feature starts interacting with others, things get a lot worse:

{% raw %}

{% endraw %}
fun max(col : Collection<Int>, compare : fun(Int, Int) : Int) : Int

{% raw %}

{% endraw %}

Have you got lost in colons? Yes, me too… So we decided to change the function type syntax to the following:

{% raw %}

{% endraw %}
fun max(col : Collection<Int>, compare : (Int, Int) -> Int) : Int

{% raw %}

{% endraw %}

And a little more Additionally, we introduced optional parentheses in types, changed the tuple syntax to be distinguishable from parenthesized expression lists and made some minor (backward compatible) changes. All this will be reflected in the docs soon. As usual, your feedback is very welcome. Stay tuned, and don’t miss the announcement next Tuesday!