# Kotlin Shape Class

**URL:** <https://discuss.kotlinlang.org/t/kotlin-shape-class/18219>\
**Category:** Uncategorized\
**Created:** [June 24, 2020, 1:09pm UTC](https://discuss.kotlinlang.org/t/kotlin-shape-class/18219 "2020-06-24T13:09:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![imanushin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/imanushin/32/5471_2.png) [@imanushin](https://discuss.kotlinlang.org/u/imanushin)\
**Post date:** [June 24, 2020, 1:09pm UTC](https://discuss.kotlinlang.org/t/kotlin-shape-class/18219/1 "2020-06-24T13:09:30Z")

</div>

Could you please help, are any Youtrack issues for shape classes support? I’d like to extend existing or create new.

There are plans for it at the .Net Core - [Exploration: Shapes and Extensions · Discussion #164 · dotnet/csharplang · GitHub](https://github.com/dotnet/csharplang/issues/164).

What features I expect:

1. Ability to extend the existing companion objects or Java Static layer with statefull logic. For example, I’d like to have something like this:

```kotlin
shape object Duration { // <-- these methods will be attached to this class
      val MAX: Duration = Duration.ofHours(999); // e.g. we extend static members of class
} 

fun getTimeout(input: String): Duration {
    return when(input) {
          ...
         else -> Duration.MAX
    }
}

```

1. Ability to omit companion objects in generic declaration

```kotlin
shape object Duration : Converable<Long, Duration> {
      override fun convert(from: Long): Duration = Duration.ofNanos(from)
} 

fun <TResult> fromLong(input: Long): TResult
  where TResult.Companion: Convertible<Long, TResult> { // so Kotlin Compiler can attach necessary factory-like objects
      return TResult.convert(input) // we called method from shape class or from companion object
}

```

---

<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:** [June 24, 2020, 1:36pm UTC](https://discuss.kotlinlang.org/t/kotlin-shape-class/18219/2 "2020-06-24T13:36:55Z")

</div>

Your first example could be easily implemented by companion extensions:

```kotlin
val Duration.Companion.MAX get() = Duration.ofHours(999)

```

The second one is about contracts on Companions. It is not possible right now, but it was discussed multiple times. I call them companion interfaces. I can’t find one clear proposal right now, but there were several discussions here.

---

<div class="post-metadata">

**Author:** ![imanushin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/imanushin/32/5471_2.png) [@imanushin](https://discuss.kotlinlang.org/u/imanushin)\
**Post date:** [June 24, 2020, 2:01pm UTC](https://discuss.kotlinlang.org/t/kotlin-shape-class/18219/3 "2020-06-24T14:01:49Z")

</div>

`Duration` class is from Java, it doesn’t have companion object.

And I pre-created value before, so right code сould be like this:

```kotlin
private val maxDuration = Duration.ofHours(999)

val Duration.Companion.MAX get() = maxDuration

```

---

<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:** [June 24, 2020, 2:16pm UTC](https://discuss.kotlinlang.org/t/kotlin-shape-class/18219/4 "2020-06-24T14:16:57Z")

</div>

For that you need [KT-11968](https://youtrack.jetbrains.com/issue/KT-11968).Pre-created values should be stored externally, because otherwise it will produce a lot of problems with initialization order (there are already a lot of them).

---

<div class="post-metadata">

**Author:** ![imanushin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/imanushin/32/5471_2.png) [@imanushin](https://discuss.kotlinlang.org/u/imanushin)\
**Post date:** [June 24, 2020, 3:04pm UTC](https://discuss.kotlinlang.org/t/kotlin-shape-class/18219/5 "2020-06-24T15:04:00Z")

</div>

Interesting. However as I can see I couldn’t attach the interface implementation to existing Companion object.

Moreover, I have explicitly put the companion object into the methods like `fun <TResult> fromLong(input: Long)` above, however compiler can resolve them automatically.

---

<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:** [June 24, 2020, 3:12pm UTC](https://discuss.kotlinlang.org/t/kotlin-shape-class/18219/6 "2020-06-24T15:12:32Z")

</div>

Companion interfaces are very important for future. They could allow to solve most of HKT use-cases and are really useful for a lot of things including serialization. But they require careful design, study and discussion.
