# Kotlin and the Expression Problem

**URL:** <https://discuss.kotlinlang.org/t/kotlin-and-the-expression-problem/1416>\
**Category:** Language Design\
**Created:** [January 17, 2016, 4:13pm UTC](https://discuss.kotlinlang.org/t/kotlin-and-the-expression-problem/1416 "2016-01-17T16:13:07Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![JanKanis](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@JanKanis](https://discuss.kotlinlang.org/u/JanKanis)\
**Post date:** [August 7, 2016, 8:17pm UTC](https://discuss.kotlinlang.org/t/kotlin-and-the-expression-problem/1416/10 "2016-08-07T20:17:08Z")

</div>

(I’m a bit late to this discussion, but the [other discussion on almost the same topic](https://discuss.kotlinlang.org/t/extension-types-for-kotlin/1390) is still active, and my comment fits better in this thread)

An important use case for a type class / extension type-like feature would be passing an object of a type, e.g. `String`, to a method that expects an interface type that `String` does not implement, e.g. `Addable`. I think it would be quite useless to require the interface to know about some Kotlin magic such as an extra hidden `self` parameter, since that would break Java interop. Then this feature would not help at all if the expected interface was a pre-existing plain old Java interface used in a Java library. While I would like to have some nice syntax in Kotlin to use type classes, I don’t think there is a way around having an implementation based on wrappers. AFAIK wrapping the `String` in a wrapper that implements `Addable` is the only Java-compatible way to implement this. And if wrappers are used for interop with Java it is probably best to use the same implementation in pure Kotlin code as well.

Regarding the problems with wrapping and equality, such a wrapper would only be passed to code that expects an `Addable` (in this example). If that code is written in Java it would be unlikely to try to compare the wrapper to a string and expect both objects to test equal because as far as this code knows `String` does not implement `Addable`. If the code is Kotlin the compiler could insert some extra magic to remove the wrapper. And besides, using wrappers is also the usual Java way to pass an object `Foo` to a method that expects a `Bar`. While it isn’t a leak-proof abstraction it often works well enough.

---

_[View the full topic](https://discuss.kotlinlang.org/t/kotlin-and-the-expression-problem/1416)._
