3

I need to implement an interface (ResultSet) having hundreds of methods. For now, I'm going to implement only a subset of these methods, throwing a NotImplementedError for the others.

In Java I found two solutions:

  1. Create an abstract class AbstractResultSet implementing ResultSet, declaring all methods to throw NotImplementedError. No hacks, but a lot of boilerplate code.
  2. Use Proxy.newProxyInstance to implement all methods together in the InvocationHandler. Less code but also less immediate to use for other coders.

Is there a third option in Kotlin?

In my case, I need to implement a a ResultSet over an IBM dataset (with packed decimals, binary fields, zoned numbers, rows with variable length, etc.) to import it in a SQLServer via SQLServerBulkCopy. I don't know which ResultSet methods are called by this class, so, for now, I'm going to implement only the "most used" methods, logging the calls to unimplemented method.

Ruan_Lopes
  • 1,381
  • 13
  • 18
Fraquack
  • 93
  • 1
  • 4

2 Answers2

3

Checkout the standard TODO function which marks a todo and also throws a NotImplementedError

/**
 * Always throws [NotImplementedError] stating that operation is not implemented.
 *
 * @param reason a string explaining why the implementation is missing.
 */
@kotlin.internal.InlineOnly
public inline fun TODO(reason: String): Nothing = throw NotImplementedError("An operation is not implemented: $reason")

Usage:

fun foo() {
        TODO("It will be soon")
    }

With this way you can also find notImplemented fetures using the IDE "todo" tab. It is a plus.

Fredy Mederos
  • 2,506
  • 15
  • 13
  • This answer is an improving of @Andrew solutions, but basically shares the same idea. I would extend the interface providing a default implementation with TODO for each method. It's simple, the IDE would do the job, but I would produce a lot of boilerplate code. – Fraquack Oct 31 '17 at 09:12
0

You can crate an Interface MyResultSet which inherit from ResultSet, and implement all methords to throw NotImplementedError. This is like your first solution in Java, but it's now an interface not a class, and give you more flexibility. Hope help.

interface MyResultSet : ResultSet {
    fun bar() ...
    fun foo() {
        throw NotImplementedError()
    }
}
Andrew
  • 1,088
  • 10
  • 21