# : Operator Please

**URL:** <https://discuss.kotlinlang.org/t/operator-please/6821>\
**Category:** Language Design\
**Created:** [February 28, 2018, 9:33am UTC](https://discuss.kotlinlang.org/t/operator-please/6821 "2018-02-28T09:33:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discuss.kotlinlang.org/u/user1)\
**Post date:** [February 28, 2018, 9:33am UTC](https://discuss.kotlinlang.org/t/operator-please/6821/1 "2018-02-28T09:33:05Z")

</div>

Please add a `:` operator that maps to `to`, so that e.g. maps can be written as  
`mapOf(1:a, 2:b, 3:c, 4:d, 5:e, 6:f, 7:g)`  
instead of  
`mapOf(1 to a, 2 to b, 3 to c, 4 to d, 5 to e, 6 to f, 7 to g)`

---

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discuss.kotlinlang.org/u/user1)\
**Post date:** [February 28, 2018, 9:40am UTC](https://discuss.kotlinlang.org/t/operator-please/6821/2 "2018-02-28T09:40:27Z")

</div>

This has been suggested in the context of collection literals for maps, but what I’m proposing here is an operator that will be defined on `Any` to return a `Pair`.

If collection literals were implemented, it might make more sense for this to be a `Pair` literal, as it differs to other operators in that it might never make sense to overload.

---

<div class="post-metadata">

**Author:** ![tieskedh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tieskedh/32/2816_2.png) [@tieskedh](https://discuss.kotlinlang.org/u/tieskedh)\
**Post date:** [February 28, 2018, 10:32am UTC](https://discuss.kotlinlang.org/t/operator-please/6821/3 "2018-02-28T10:32:26Z")

</div>

I like it more if it returns a map.entree as this can be more useful with the forEach with receivers

> [@forEach with elements as the receivers](https://discuss.kotlinlang.org/t/foreach-with-elements-as-the-receivers/6415):
>
> I wonder why an equivalent of forEach with elements as the receivers was not included in the Standard library. public inline fun \<T\> Iterable\<T\>.each(action: T.() -\> Unit): Unit { for (element in this) element.action() } There clearly do exist cases when iterating elements as receivers of an action is the most concise and readable code.

---

<div class="post-metadata">

**Author:** ![mattquigley](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mattquigley/32/2943_2.png) [@mattquigley](https://discuss.kotlinlang.org/u/mattquigley)\
**Post date:** [March 5, 2018, 4:55am UTC](https://discuss.kotlinlang.org/t/operator-please/6821/4 "2018-03-05T04:55:54Z")

</div>

Collection literals was the most requested feature from the language survey: [https://blog.jetbrains.com/kotlin/2017/06/kotlin-future-features-survey-results/](https://blog.jetbrains.com/kotlin/2017/06/kotlin-future-features-survey-results/)

Your example, though, probably won’t cut it as the `:` symbol represents a type definition. Python parity is not a goal of Kotlin 😉

---

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discuss.kotlinlang.org/u/user1)\
**Post date:** [March 5, 2018, 10:54am UTC](https://discuss.kotlinlang.org/t/operator-please/6821/5 "2018-03-05T10:54:43Z")

</div>

It could be a collection literal for `Pair`.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [March 6, 2018, 3:18pm UTC](https://discuss.kotlinlang.org/t/operator-please/6821/6 "2018-03-06T15:18:18Z")

</div>

The only way would be embedded in a collection literal. There would be no way to differentiate

```
someMethodAcceptingPairs(key1: value1, key2: value2)

```

from Kotlin named parameters:

```
someMethod(paramName1: value1, paramName2: value2)

```

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 6, 2018, 3:39pm UTC](https://discuss.kotlinlang.org/t/operator-please/6821/7 "2018-03-06T15:39:31Z")

</div>

I am 99% sure that named parameters in Kotlin use `=` instead of `:`

```kotlin
someMethod(paramName = value1, paramName2 = value2)

```

btw: how do you change the color? 😊

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [March 6, 2018, 3:48pm UTC](https://discuss.kotlinlang.org/t/operator-please/6821/8 "2018-03-06T15:48:09Z")

</div>

Doh! Sorry, been doing a lot of Groovy coding in gradle files lately and it uses : for that

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [March 6, 2018, 4:55pm UTC](https://discuss.kotlinlang.org/t/operator-please/6821/9 "2018-03-06T16:55:47Z")

</div>

Use **```kotlin**. It is somewhere in one of markdown standards.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 6, 2018, 5:49pm UTC](https://discuss.kotlinlang.org/t/operator-please/6821/10 "2018-03-06T17:49:12Z")

</div>

Ok after some experimenting I have it kind of working. Kotlin needs to be spelled with a capital K and there needs to be a space between ` ` ` and Kotlin. But even this is not working all the time … Doesn’t really matter. I thought @dalewking made them red intentionally to highlight them.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [March 7, 2018, 1:08am UTC](https://discuss.kotlinlang.org/t/operator-please/6821/11 "2018-03-07T01:08:28Z")

</div>

I just said that it was code and it apparently highlighted them red because they were not valid.

---

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discuss.kotlinlang.org/u/user1)\
**Post date:** [March 13, 2018, 7:42am UTC](https://discuss.kotlinlang.org/t/operator-please/6821/12 "2018-03-13T07:42:41Z")

</div>

The downside is that it would be preclude a standard ternary operator (unless it only worked within map literals), because a ternary operator would short-circuit, but a Pair constructor would evaluate both arguments.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [March 13, 2018, 8:57am UTC](https://discuss.kotlinlang.org/t/operator-please/6821/13 "2018-03-13T08:57:47Z")

</div>

I had some groovy background when I came to Kotlin and at first, I also wanted `:` operator for maps, but after some time it seems that it would brake to many things, and collection literals are overrates. But I wanted to point to another possibility. One of the most brilliant things about kotlin is a possibility to declare some behaviors in a specific context. For example to redefine some operators for specific receivers. For example one could make this:

```kotlin
class MapBuilder{
  private val map = HashMap<String,Any>()
  operator fun String.rem(obj: Any){
    map.put(this, obj)
  }
}

fun buildMap(action: MapBuilder.()->Unit): Map<String, Any>{
  return MapBuilder().apply(action)
}

val myMap = buildMap{
  "a" % 22
  "b" % "this is a string"
}

```

I do not say that this particular way is good, but it could be done. The current state is not bad since typing `to` requires exactly the same number of key presses as typing `:`.

The only thing I can see to improve the situation is to add some additional operator which is not used by now like `~` or `^`, do not implement it by default and reserve it for attribution and DSLs.

---

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discuss.kotlinlang.org/u/user1)\
**Post date:** [March 13, 2018, 12:56pm UTC](https://discuss.kotlinlang.org/t/operator-please/6821/14 "2018-03-13T12:56:14Z")

</div>

> [@darksnake](#):
>
> typing `to` requires exactly the same number of key presses as typing `:`

It’s not the typing I’m trying to improve, it’s the reading!

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [March 13, 2018, 1:12pm UTC](https://discuss.kotlinlang.org/t/operator-please/6821/15 "2018-03-13T13:12:06Z")

</div>

I think that reading suffer in this case only because previous experience with Python/Groovy. I do not think that it is the aim of the JetBrains team to simplify migration from python.  
There is a workaround to use more concise syntax for json building in kotlin-js. I’ve shown it [here](https://discuss.kotlinlang.org/t/builder-for-dynamic-objects/6077), It allows to use `=` instead of `to` and avoid quotes, but I do not think it is possible or even favorable for kotlin-jvm.

---

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discuss.kotlinlang.org/u/user1)\
**Post date:** [March 13, 2018, 2:07pm UTC](https://discuss.kotlinlang.org/t/operator-please/6821/16 "2018-03-13T14:07:21Z")

</div>

I’ve never programmed in Groovy (except Gradle builds) and only a little Python. My familiarity with `:` in this context is from standard English more than any particular programming/markup language, although it’s a very common convention there as well.

If you compare classic Gradle scripts to the equivalent in Kotlin script, you can see how the lack of syntax for literal maps can really make things ugly.

---

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discuss.kotlinlang.org/u/user1)\
**Post date:** [March 13, 2018, 2:12pm UTC](https://discuss.kotlinlang.org/t/operator-please/6821/17 "2018-03-13T14:12:52Z")

</div>

`:` isn’t an issue of one vs. two characters, that would be trivial. It’s really about standard punctuation vs. a word. If you don’t think that’s siginificant, read a telegram with STOPs in place of periods 🙂

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 13, 2018, 2:16pm UTC](https://discuss.kotlinlang.org/t/operator-please/6821/18 "2018-03-13T14:16:18Z")

</div>

I’d like to point out that the original post was about a `:` operator in general, not only in connection to collection literals.  
I personally prefer `to` to `:` in that case. That does not mean that I would not welcome any sort of collection literal. I definitely prefer `{...}` over `mapOf(...)` but I guess this comes down to personal preferences.

> [@user1](#):
>
> My familiarity with : in this context is from standard English more than any particular programming/markup language

I can’t think of an example where `:` would be used in English in any comparable way as suggested here. Can you give an example of that?

---

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discuss.kotlinlang.org/u/user1)\
**Post date:** [March 13, 2018, 2:21pm UTC](https://discuss.kotlinlang.org/t/operator-please/6821/19 "2018-03-13T14:21:17Z")

</div>

This year you will have the following teachers:  
Mr A: Subject 1  
Mrs B: Subject 2  
Mr C: Subject 3  
Mrs D: Subject 4

That’s a typical use in English, and is conceptually the same as `to`.

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [March 13, 2018, 2:23pm UTC](https://discuss.kotlinlang.org/t/operator-please/6821/20 "2018-03-13T14:23:01Z")

</div>

People are way more flexible than compilers when reading something. People can switch contexts with very little effort from one part of a text to another.

You can of course build a compiler that can also switch contexts, but it won’t be able to do it without effort. And this extra effort will result in longer compilation times. So you need to make trade-offs.

If I use a colon for this feature, I won’t be able to use it for another. Which feature is more important, i.e. will make the language more pleasant, easier to parse and compile, etc.?

In this case the Kotlin team chose to use the colon for type specifications. I guess because type specifications occur more frequently in code than map entries.
