Skip to content

Instantly share code, notes, and snippets.

@hanslovsky
Last active August 9, 2020 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanslovsky/39ece441a74ba2ce7e5c33c6f302efb0 to your computer and use it in GitHub Desktop.
Save hanslovsky/39ece441a74ba2ce7e5c33c6f302efb0 to your computer and use it in GitHub Desktop.
kotlin ambiguous overloads test
import net.imglib2.*
import net.imglib2.img.Img
import net.imglib2.img.array.ArrayImgs
interface _A
interface _B
interface _AB: _A, _B
interface _BA: _B, _A
class A : _A
class B : _B
class AB: _AB
class BA: _BA
fun <T: _A> run(t: T) = println("runA")
fun <T: _B> run(t: T) = println("runB")
fun <T: _AB> run(t: T) = println("runAB")
fun <T: _BA> run(t: T) = println("runBA")
fun <T> func(i: IterableInterval<T>) = println("II")
fun <T> func(i: IterableRealInterval<T>) = println("IRI")
fun <T> func(i: RandomAccessibleInterval<T>) = println("RAI")
fun <T> func(i: RandomAccessible<T>) = println("RA")
fun <T> func(i: RealRandomAccessibleRealInterval<T>) = println("RRARI")
fun <T> func(i: RealRandomAccessible<T>) = println("RRA")
// clashes with fun <T> func(i: RandomAccessibleInterval<T>) w/same JVM signature error:
// fun <T, R> func(i: R) where R: RandomAccessibleInterval<T>, R: IterableInterval<T> = println("RAI & II")
// clashes with fun <T> func(i: IterableInterval<T>) w/same JVM signature error:
// fun <T, R> func(i: R) where R: IterableInterval<T>, R: RandomAccessibleInterval<T> = println("RAI & II")
fun <T> func(i: Img<T>) = println("Img")
fun<T> IterableInterval<T>.extFunc() = func(this)
fun<T> RandomAccessibleInterval<T>.extFunc() = func(this)
fun<T> RandomAccessible<T>.extFunc() = func(this)
class SomeRAI<T>(rai: RandomAccessibleInterval<T>) :
RandomAccessibleInterval<T> by rai, RandomAccessible<T>
fun main() {
run(A())
run(B())
run(AB())
run(BA())
run(BA() as _A)
run(BA() as _B)
println()
val img = ArrayImgs.ints(1L)
val rai = SomeRAI(img)
// Cannot call anything that is RandomAccessibleInterval & IterableInterval, e.g:
// Cannot choose among the following candidates without completing type inference:
// public final fun <A : Any!, B : Type<IntType!>!> convert(p0: IterableInterval<IntType!>!, p1: Converter<in IntType!, in IntType!>!, p2: IntType!): IterableInterval<IntType!>! defined in net.imglib2.converter.Converters
// public final fun <A : Any!, B : Type<IntType!>!> convert(p0: RandomAccessible<IntType!>!, p1: Converter<in IntType!, in IntType!>!, p2: IntType!): RandomAccessible<IntType!>! defined in net.imglib2.converter.Converters
// public final fun <A : Any!, B : Type<IntType!>!> convert(p0: RandomAccessibleInterval<IntType!>!, p1: Converter<in IntType!, in IntType!>!, p2: IntType!): RandomAccessibleInterval<IntType!>! defined in net.imglib2.converter.Converters
// Converters.convert(img, Converter { s: IntType, t: IntType -> }, IntType())
// func(img)
// img.extFunc()
func(img)
// Will have to cast explicitly if fun <T> func(i: Img<T>) is not specified:
func(img as IterableInterval<*>)
func(img as RandomAccessibleInterval<*>)
(img as IterableInterval<*>).extFunc()
(img as RandomAccessibleInterval<*>).extFunc()
// RAI + RA works without a problem
func(rai)
rai.extFunc()
}
runA
runB
runAB
runBA
runA
runB
Img
II
RAI
II
RAI
RAI
RAI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment