Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typealias - Combine Multiple Interfaces in Kotlin

Tags:

java

kotlin

I'm a bit rusty with protocol composition in Kotlin, I'd just like to combine multiple interfaces by declaring a custom typealias:

// This doesn't work
typealias MyType = (ReadableInterface && WritableInterface)

Any ideas?


In Swift, I would do it this way:

typealias MyType = ReadableInterface & WritableInterface

In Objective C, I would do it this way:

typedef <ReadableInterface, WritableInterface> MyType; 
like image 202
Mick Avatar asked Nov 26 '19 05:11

Mick


People also ask

Can you implement multiple interfaces in Kotlin?

A class in Kotlin can implement as many interfaces as they like but it can only extend from one abstract class.

How do you use Kotlin Typealias?

Typealias usage in multi-platform projects To make this easier, Kotlin provides a mechanism of expected and actual declarations. The interfaces in the common code are the expected declarations, defined using the expect keyword. The implementation in the platform code is defined using the actual keyword.


1 Answers

Why not just create new interface?

interface MyType : ReadableInterface, WritableInterface
like image 156
Evgeny Bovykin Avatar answered Oct 09 '22 15:10

Evgeny Bovykin