# Data interfaces!

**URL:** <https://discuss.kotlinlang.org/t/data-interfaces/25278>\
**Category:** Language Design\
**Created:** [July 26, 2022, 12:51am UTC](https://discuss.kotlinlang.org/t/data-interfaces/25278 "2022-07-26T00:51:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![smmh](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@smmh](https://discuss.kotlinlang.org/u/smmh)\
**Post date:** [July 26, 2022, 12:51am UTC](https://discuss.kotlinlang.org/t/data-interfaces/25278/1 "2022-07-26T00:51:27Z")

</div>

Imagine we have a sealed interface called `Point` that has two fields, `x: Double` and `y:Double`, and one sub-interface called `MutablePoint`. We also have a `private data class Impl` for both `Point` and `MutablePoint`, and we’ve made them data classes, thinking we’re taking advantage of Kotlin’s cool features.

But then this test fails:

```kotlin
@Test
fun equalityTest() {
    val immutable = Point.of(5, 6)
    val set = setOf(immutable)
    val mutable = MutablePoint.of(5, 6)
    assertTrue(mutable in set)
}

```

And so you realize that the only features that data classes inject into your code, that is having conveniently defined `equals`, `hashCode` and `toString` methods as well as `componentN` methods, only matter if the class is public and its objects are working with objects of the same class, and they’re useless if they’re private and their core logic is defined in the interfaces they implement, like the example above.

The current solution for this is make the classes non-data, and make them extend a common superclass like `AbstractPointImpl` where the `equals`, `hashCode` are defined in a way that allows the subclasses co-exist together in collections. This is bad because 1) we stopped using data classes and with it we lost the compile-time and runtime optimizations that exist for them and 2) we introduced a whole new class within which we had to resort to ancient modes of programming that is very reminiscent of the Java days, something that Kotlin was trying to put behind especially with data classes.

What I propose is simple: data interfaces! They are just like data classes except for interfaces. **They provide `equals`, `hashCode` logic based on a handful of fields defined in the parenthesis after the `data interface` and the interface name.** They could also provide predefined `toString` and `componentN` behavior but this is optional and not the subject of this post.

Known issue: I realize this won’t work because the JVM does not allow `equals` and `hashCode` to be defined in interfaces, because those methods are traditionally a part of `Object` which all classes implicitly extend, and so interfaces do not have access to them to override them.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [July 26, 2022, 1:31am UTC](https://discuss.kotlinlang.org/t/data-interfaces/25278/2 "2022-07-26T01:31:53Z")

</div>

Data class is really just a simple syntactic sugar to save boilerplate for the most common cases. If your case is not the common one, feel free to implement the same manually in 5 minutes or so (actually, generate the code by IDE).

Also note comparing objects of different types is tricky. If you have another class implementing `Point` then it would probably didn’t make sense that `Point.of(5, 6) == 3DPoint.of(5, 6, 2)` returns true (just an example). You said your `Point` is sealed, so in that case it actually makes sense, but I think it is a rather rare case.

---

<div class="post-metadata">

**Author:** ![TenebrisUltorem](https://avatars.discourse-cdn.com/v4/letter/t/47e85d/32.png) [@TenebrisUltorem](https://discuss.kotlinlang.org/u/TenebrisUltorem)\
**Post date:** [December 20, 2023, 11:22am UTC](https://discuss.kotlinlang.org/t/data-interfaces/25278/3 "2023-12-20T11:22:38Z")

</div>

I sounds good for equals, hashCode and toString methods, but if you need to copy data class when you access it by interface, it turns into total crap.

For example we have

```auto
sealed interface SomeData {
    val id: Int
    val data: String
    val lastAccessDate: LocalDate
}

data class SomeDataImpl(
    override val id: Int, 
    override val data: String,
    override val lastAccessDate: LocalDate
): SomeData

data class SomeMoreComplexData(
    override val id: Int,
    override val data: String,
    override val lastAccessDate: LocalDate
    val comments: String
): SomeData

```

If we want to modify all the data by accessing it via interface, we’ll have to do something lile that

```auto
fun main() {
    val dataList = listOf(
        SomeDataImpl(
            1, "Data", LocalDate.now()),
        SomeMoreComplexData(
            2,
            "ComplexData",
            LocalDate.now(),
            "This data is too complex to be stored at SomeDataImpl")
    )

    val updatedDataList = dataList.map { 
        when (it) {
            is SomeDataImpl -> it.copy(lastAccessDate = LocalDate.now())
            is SomeMoreComplexData -> it.copy(lastAccessDate = LocalDate.now())
        }
    }
}

```

instead of simply

```auto
val updatedDataList = dataList.map { it.copy(lastAccessDate = LocalDate.now()) }

```

When there are more data to modify and more interface implementations, that will be much more terrible. Data interface could be a salvation in this situation

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [December 24, 2023, 3:46am UTC](https://discuss.kotlinlang.org/t/data-interfaces/25278/4 "2023-12-24T03:46:41Z")

</div>

The problem with this is that the two objects are _not_ equal:

```auto
val immutable: Point = Point.of(5, 6)

val mutable: MutablePoint = MutablePoint.of(5, 6)

```

Even though `MutablePoint` extends `Point`, it’s not the same type; if you try to check if a `MutablePoint` instance equals a `Point` instance, that’s _always_ going to be false, because they’re not the same type. The data class equals is just a convenience function that compares the values of two objects of the _same_ type. If you have two different data classes, `Foo` and `Bar`, and check if an instance of `Foo` is equal to an instance of `Bar`, even if they have the same fields, they’re not gonna equal each other, because they’re different classes.

---

<div class="post-metadata">

**Author:** ![TenebrisUltorem](https://avatars.discourse-cdn.com/v4/letter/t/47e85d/32.png) [@TenebrisUltorem](https://discuss.kotlinlang.org/u/TenebrisUltorem)\
**Post date:** [January 4, 2024, 2:36pm UTC](https://discuss.kotlinlang.org/t/data-interfaces/25278/5 "2024-01-04T14:36:01Z")

</div>

And what is the problem? They are of different type and are not equal

---

<div class="post-metadata">

**Author:** ![unknownjoe796](https://avatars.discourse-cdn.com/v4/letter/u/58956e/32.png) [@unknownjoe796](https://discuss.kotlinlang.org/u/unknownjoe796)\
**Post date:** [January 4, 2024, 3:14pm UTC](https://discuss.kotlinlang.org/t/data-interfaces/25278/6 "2024-01-04T15:14:17Z")

</div>

I think the idea makes sense. Yeah, it won’t have `copy`, but that wasn’t the point here. The point was to make good defaults to the implementing classes for hashCode and equals.

And yes, you can define `MutablePoint` and `Point` to be equal when their contents are equal and it does make sense. Kotlin follows this pattern for `List` and `MutableList`.

Try this in the REPL: `listOf(1, 2) == mutableListOf(1, 2)`

I do wonder, however, if we’re now straying pretty far into the idea of just tracking mutability of objects.

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [January 5, 2024, 6:01am UTC](https://discuss.kotlinlang.org/t/data-interfaces/25278/7 "2024-01-05T06:01:59Z")

</div>

I think I was replying to the original post, not realising it’s from over a year ago.

---

<div class="post-metadata">

**Author:** ![lordnik22](https://avatars.discourse-cdn.com/v4/letter/l/bbce88/32.png) [@lordnik22](https://discuss.kotlinlang.org/u/lordnik22)\
**Post date:** [April 8, 2025, 1:37pm UTC](https://discuss.kotlinlang.org/t/data-interfaces/25278/8 "2025-04-08T13:37:22Z")

</div>

You could pack this when-expression into an extension function for the interface:

```auto
fun SomeData.copy(id: Int = this.id, ...) = when (it) {
    is SomeDataImpl -> it.copy(lastAccessDate = LocalDate.now())
    is SomeMoreComplexData -> it.copy(lastAccessDate = LocalDate.now())
}

```

This would at least give the same look and feel.
