# Replace Interface with Type

**URL:** <https://discuss.kotlinlang.org/t/replace-interface-with-type/27504>\
**Category:** Language Design\
**Created:** [October 17, 2023, 3:13am UTC](https://discuss.kotlinlang.org/t/replace-interface-with-type/27504 "2023-10-17T03:13:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefanus.ayudha](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/stefanus.ayudha/32/10729_2.png) [@stefanus.ayudha](https://discuss.kotlinlang.org/u/stefanus.ayudha)\
**Post date:** [October 17, 2023, 3:13am UTC](https://discuss.kotlinlang.org/t/replace-interface-with-type/27504/1 "2023-10-17T03:13:26Z")

</div>

I’ve been years in java and kotlin, i don’t really enjoy using “interface” since the usage is often ambiguous. In common scenario i do use “interface” as a contract for open interface. But in general, i do need to define “type” , “pattern/structure”, “interface”. And the longer i code, the more i need “type” or “pattern” instead of “interface”. It (interface) does works for declaring type and pattern, but it ambiguous.

I highly suggest to use “type” since type can contain interface contract, and can also works as pattern declaration.

Im not a fan of inheritance, open class, abstract class, no body does imo. I prefer using pattern and pattern segregation. What is interface segregation anyway.

instead of making dog, inherit to mamal, inherit to vertebrate, inherit to animal.

it’s easier to classify things using pattern and type.  
ex: dog should have animal pattern, mammal pattern, vertebrate pattern. and in some level layer we don’t care whether dog is vertebrate or not.

its easier to work with group theory, how we group things, how classification works.

for example in android, if i put streamable contract to a fragment, and send the fragment. how do i know that “streameable” is actualy a fragment without casting it?

but with pattern segregation, let say i have “interface Fragment” all i need to do is to segregate the pattern with “interface Streamable” contract

and now i have `interface StreamableFragment: Fragment, Streamable`

now i know that StreamableFragment is a Fragment, by pattern recognition.  
But since Fragment in android is a open class im unable to do this.

And so, by using “type” as pattern declaration, we force developer to not doing open class or abstract class. but use pattern segregation instead.

I also hate open class and abstract, everybody does imo. why would we modify an object? we r not modifying a fish to become a mammal. why would we do that.

Builder-pattern can do all that. No complicated inheritance needed… Just Pattern and Builder.

this is my opinion based on code experience.

---

<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:** [October 17, 2023, 7:05am UTC](https://discuss.kotlinlang.org/t/replace-interface-with-type/27504/2 "2023-10-17T07:05:48Z")

</div>

What is the “pattern”? “interface” is just a name, you can think that in your code “patterns” are represented as interfaces. Everybody knows what “interface” is, we are all used to that naming and again, I personally don’t even know what you mean by “pattern”.

Also, what is the change you request exactly? Split “interface” into “interface”, “pattern” and “type” which all do exactly same thing?

The rest of your post is not really related to the language, but to your code and its design. You can structure your `Dog` or `StreamableFragment` however you like. Language doesn’t force you to use abstract classes neither.

---

<div class="post-metadata">

**Author:** ![Melchior.Grutzmann](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/melchior.grutzmann/32/8159_2.png) [@Melchior.Grutzmann](https://discuss.kotlinlang.org/u/Melchior.Grutzmann)\
**Post date:** [October 20, 2023, 5:08pm UTC](https://discuss.kotlinlang.org/t/replace-interface-with-type/27504/3 "2023-10-20T17:08:07Z")

</div>

I think that your observed “ambiguity” of an `interface` is intentional, i.e. via an interface you specify what is available (points of usability), neither how it is used nor how exactly it is implemented. This is part of the abstraction scheme.

In your _patterns_, on the other hand, you want to establish how to approach the solution. If you have been working with Scala before, you may know `mixin`s, i.e. given minimal data how do you implement part of its functionality. This can also be emulated with default method implementations in certain interfaces.

I do not understand your _type_. In Kotlin (at least on the JVM) you can use all `interface`s, `data class`es and `class`es as abstract types, i.e. hand elements that are only restricted by these _type_s, assign them to (mutable) variables, and operate on proper implementations (without knowing their details).

A `typealias` in Kotlin is the minimal version of a concrete type duplication, i.e. it is just a nickname for a concrete type. The advantage comes in refactoring, i.e. if you first start out with a `typealias` and later replace it by an(other) implementation. In that case, you just have to drop in the definition (and possibly the call of constructors and converters), all the other stuff remains syntactically the same.

---

<div class="post-metadata">

**Author:** ![alkurop](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@alkurop](https://discuss.kotlinlang.org/u/alkurop)\
**Post date:** [October 22, 2023, 9:15am UTC](https://discuss.kotlinlang.org/t/replace-interface-with-type/27504/4 "2023-10-22T09:15:16Z")

</div>

As I understand, instead of extending abstract class what you would like to do is have `mixin` just like in Scala.  
Now the problem is how to emulate that in Kotlin.  
Let’s say we have

```auto
interface WalkingAnimal {
    fun walk(){ ...impl ...}
    val limbs:Limbs
}

interface NoisyAninal {
    fun makeSomeNoize(){ ... impl ... }
    val voice:Voice
}

```

Now how we make

```auto
class Dog : NoisyAnimal, WalkingAnimal .

```

Basically this is fine, no problem because interface can have a method implementation.

Actually I would even better put the impl functionality as extensions:

```auto
interface WalkingAnimal {
    val limbs: Limbs
}

fun WalkingAnimal.walk() { ... impl ... }

```

As for the `StreamableFragment` example - I find this class questionable, because it causes inheritance over composition eventually. I never liked the way `BaseActivity` or `BaseFragment` in android are fathers extended, adding features on top of existing features every new layer. Maybe it’s possible to overcome inheritance hell with just extension functions, or extension functions separated from interface. Left alone Android stuff, because it’s legacy Java code basically.
