Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save konrad-jamrozik/3bdf9a306fea6a2e25f42ee8b4bc5aa8 to your computer and use it in GitHub Desktop.
Save konrad-jamrozik/3bdf9a306fea6a2e25f42ee8b4bc5aa8 to your computer and use it in GitHub Desktop.
// I want to do the following:
val myTable1 = MyTable()
// where MyTable inherits from ImmutableTable [1], or at least Table [2], 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.build()
// i.e. I do not want to be forced to use companion objects / static factory methods.
// Reference:
// [1] ImmutableTable source: http://google.github.io/guava/releases/snapshot/api/docs/src-html/com/google/common/collect/ImmutableTable.html#line.45
// [2] Guava immutable collections: https://github.com/google/guava/wiki/NewCollectionTypesExplained#table
// Here is what I tried:
class MyTable_Attempt1 : ImmutableTable<Int, Int, Int> // ERROR: This type has a constructor, and thus must be initialized here
{
override fun rowMap(): ImmutableMap<Int, MutableMap<Int, Int>>? {
throw UnsupportedOperationException()
}
override fun columnMap(): ImmutableMap<Int, MutableMap<Int, Int>>? {
throw UnsupportedOperationException()
}
override fun size(): Int {
throw UnsupportedOperationException()
}
}
class MyTable_Attempt2(val table: ImmutableTable<Int, Int, Int>): Table<Int, Int, Int> by table
{
// Note: I cannot delegate to ImmutableTable, getting ERROR: Only interfaces can be delegated to
init {
table = ImmutableTable.of() // ERROR: Val cannot be reassigned
}
}
class MyTable_Attempt3(var table: ImmutableTable<Int, Int, Int> ): Table<Int, Int, Int> by table
{
// Note: I cannot delegate to ImmutableTable, getting ERROR: Only interfaces can be delegated to
init {
table = ImmutableTable.of()
// Well OK, it works, but then table can be reassigned later on, as it is "var" not "val" :(
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment