# Type conversions

**URL:** <https://discuss.kotlinlang.org/t/type-conversions/828>\
**Category:** Uncategorized\
**Created:** [May 3, 2013, 2:33am UTC](https://discuss.kotlinlang.org/t/type-conversions/828 "2013-05-03T02:33:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [May 3, 2013, 2:33am UTC](https://discuss.kotlinlang.org/t/type-conversions/828/1 "2013-05-03T02:33:13Z")

</div>

I'd like to be able to write a function that accepts mutiple parameters but only of a certain types. Let's say that String and Int are allowed but not Double:

```kotlin
foo("a", 1) // compiles
foo("b", 2.0) // won't compile, doubles are not allowed
```

In Scala, I would define implicits for each valid type, these implicits would convert to MyObject and my function would simply accept MyObject parameters.

I can’t come up with a way to do this in Kotlin. The first step seems to be to define extension functions on String and Int, but past this, I’m stuck.

Is there a way that I’m no seeing? Such a feature would go a long way toward making DSL’s less clutterered…

---

<div class="post-metadata">

**Author:** ![B7W](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@B7W](https://discuss.kotlinlang.org/u/B7W)\
**Post date:** [May 3, 2013, 9:13am UTC](https://discuss.kotlinlang.org/t/type-conversions/828/2 "2013-05-03T09:13:06Z")

</div>

May be simple overloading will help you?

``

```kotlin
fun sum(a:Int, b:Int) = a + b
fun sum(a:Int, b:String) = sum(a, b.toInt())
fun main(args : Array<String>) {
  assert(sum(2, 2) == sum(2, “2”))
}
```

---

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [May 3, 2013, 4:12pm UTC](https://discuss.kotlinlang.org/t/type-conversions/828/3 "2013-05-03T16:12:05Z")

</div>

That's not good enough for DSL's, vararg support is mandatory.

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [May 3, 2013, 5:22pm UTC](https://discuss.kotlinlang.org/t/type-conversions/828/4 "2013-05-03T17:22:50Z")

</div>

Kotlin has no implicits, so there's no way of doing this for library types such as String and Int

---

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [May 3, 2013, 5:50pm UTC](https://discuss.kotlinlang.org/t/type-conversions/828/5 "2013-05-03T17:50:47Z")

</div>

That's what I thought, thanks Andrey.

I was playing with the idea of providing some limited implicit support (only for type conversion). Since it’s so easy to define extension functions in Kotlin, the compiler could look for functions with a certain signature, e.g.

```kotlin

fun to() : T

```

For example, I could define an implicit conversion from Int to MyClass:

```kotlin

fun Int.to() : MyClass {
  return new MyClass(this)
}

```

and then a function taking a MyClass can be passed an Int:

```kotlin

fun f(mc: MyClass) {
  ...
}

f(3); // will be converted automatically

```

I think the argument "We can't figure out what's going on" is silly, especially in languages that support extension functions. We have great IDE support already which can tell us everything we need to know without having to browse anything. For example, putting the cursor inside the parenthesis at the call site and pressing Ctrl-p would display all the signatures, both with explicit and implicit parameters.

The reason why I’m suggesting this is that Kotlin is emphasizing its support for DSL but I feel that without this kind of conversion support, DSL’s are severely crippled.

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [May 4, 2013, 7:57am UTC](https://discuss.kotlinlang.org/t/type-conversions/828/6 "2013-05-04T07:57:52Z")

</div>

Kotlin is deliberately designed to have no implicit conversions, the closest our feature exploration got to that is a provisional "explicit conversion operator": ~x (compiled to x.to()), so that you can always see where a conversion is performed.

There’s a technical problem with this kind of solution though (as well as the one you propose): two ordnary functions that differ only in the return type, to(): T and to(): T1, can not reside in the same class in Kotlin, becauseno overload resolution can discriminate the two. We could in principle hack the typechecker to skip this rule for functions named “to”, but this doesn’t seem exactly like a clean solution. And you wouldn’t be happy anyways, because the conversion is explicit.

---

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [May 4, 2013, 3:59pm UTC](https://discuss.kotlinlang.org/t/type-conversions/828/7 "2013-05-04T15:59:16Z")

</div>

Yes, I understand the limitations. I don't feel too strongly about this feature since I don't write a lot of DSL's myself, but I just thought I should point out that if one of Kotlin's claim to fame is DSL, then the absence of automatic conversions limits what can be done in this area. Instead of writing

myDsl { “a”, 1, ‘c’ }

I have to write:

myDsl { “a”.toObj(), 1.toObj(), ‘c’.toObj() }

or something along these lines.

Obviously, you can also have your closures accept vararg Any arguments and perform runtime casts. Not as elegant, but it keeps the DSL clean.

Overall, I have always been ambivalent about type conversions, probably because I was traumatized by them in C++ 20 years ago. However, I have softened up a bit on the topic because today’s IDE make it trivial to find out what’s going on, which wasn’t the case writing C++ on emacs in the 90’s, especially if you keep the design of type conversions very narrowly focused (Scala’s implicits are very powerful but since they are used to do more than type conversions, the result can lead to very confusing code).

If I could find other examples than DSL creation where type conversions can lead to elegant and readable code, I’d weigh in their favor more forcefully. Still looking.

---

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [May 10, 2013, 4:46pm UTC](https://discuss.kotlinlang.org/t/type-conversions/828/8 "2013-05-10T16:46:05Z")

</div>

Well, here's another useful example for type conversions, posted just a few days after my initial post:

[http://devnet.jetbrains.com/thread/444423?tstart=0](http://devnet.jetbrains.com/thread/444423?tstart=0)

I think the data in support of type conversions is piling up.

---

<div class="post-metadata">

**Author:** ![dodyg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dodyg/32/1451_2.png) [@dodyg](https://discuss.kotlinlang.org/u/dodyg)\
**Post date:** [May 11, 2013, 5:59pm UTC](https://discuss.kotlinlang.org/t/type-conversions/828/9 "2013-05-11T17:59:48Z")

</div>

I am not the target for DSL capability of Kotlin but I'd like to see more diverse DSL scenarios than just these JSON DSLs.
