Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Kotlin, how to delegate to an interface and provide only a no-arg public constructor?

Tags:

kotlin

guava

The problem arises from the fact that Kotlin class delegation allows to delegate only to constructor parameters, thus seemingly forcing you to provide constructor with an argument.

Below is my original question pertaining to a concrete use case of this problem.


I want to do the following:

val myTable1: MyTable = MyTable()

where

  • MyTable inherits from ImmutableTable (src) or at least Table
  • and I do not have to manually delegate all the Table methods to some base implementation.

I also want to avoid the following:

val myTable2: MyTable = MyTable.build() 

i.e. I do not want to be forced to use companion objects / static factory methods.

I tried to extend ImmutableTable, but I am getting This type has a constructor, and thus must be initialized here.

I tried to extend Table interface and delegate to it (to avoid reimplementing methods) but then I am forced to provide an instance of Table as constructor parameter. I cannot just initialize it in init {} block.

Please see this gist for my exact attempts.

Kotlin version used: 1.0.2

like image 368
Konrad Jamrozik Avatar asked Jun 02 '16 13:06

Konrad Jamrozik


People also ask

How to use delegate in Kotlin?

Delegation is used in Kotlin with the help of “by” keyword. • Explicit delegation: Supported by all object-oriented language and it is done by passing a delegate (the one to be implemented) object to delegating object (the one that will implement delegate object). • Implicit delegation: Requires language-level support for the delegation pattern.

What is inter interface in Kotlin?

Interfaces in Kotlin can contain declarations of abstract methods, as well as method implementations. What makes them different from abstract classes is that interfaces cannot store state. They can have properties but these need to be abstract or to provide accessor implementations. An interface is defined using the keyword interface

What is the difference between inheritance and delegation in Kotlin?

• Implicit delegation: Requires language-level support for the delegation pattern. As we know that in Kotlin, inheritance provides us with a permanent static relationship between objects which are not mutable while delegation is, this fact makes Delegation an extremely powerful alternative.

What is iwalking in Kotlin and how does it work?

Kotlin provides native capabilities to implement the delegation pattern without the need to write any boilerplate code. This is only working on interfaces (in other words abstract classes). The interface implementation of IWalking is done as shown in the following code.


Video Answer


1 Answers

As mentioned in comments, Guava has ForwardingTable that can accomplish this. But here's another option that should work even for interfaces where there isn't a "forwarding" version defined.

class MyTable private constructor(table: Table<Int, Int, Int>) : Table<Int, Int, Int> by table {

    constructor() : this(TreeBasedTable.create()) // or a different type of table if desired

}
like image 148
Paul Blessing Avatar answered Sep 25 '22 21:09

Paul Blessing