Ok I see now. It was working on Scala 2.11 but not on higher versions, because then those two implicits are ambiguous. Because Source
is also AutoCloseable
. You solve that by assigning lower priority to one of those implicits.
trait LowerPriority {
implicit object AutoCloseableIsReleasable extends Releasable[AutoCloseable] {
def release(resource: AutoCloseable): Unit = resource.close()
}
}
object Releasable extends LowerPriority {
implicit object SourceIsReleasable extends Releasable[Source] {
def release(resource: Source): Unit = resource.close()
}
}
Probably a def
with a type parameter automatically has lower priority and that’s why the other solution also worked.