I want to ensure that a method call will fail at compilation time (using Dotty 3.0.0-RC1).
Code is provided at the end. More specifically I have a method:
def do1[T <: IsA](t:T): Unit = ()
So this should work:
UseBaseA.do1(IsA())
but this should not because IsA is not a subtype of IsB:
UseBaseA.do1(IsB()) // Not failing
However it fails at run-time with a java.lang.AbstractMethodError
.
If I use an abstract type member however, I can generate a compile error.
In the code below compilation will fail here:
UseBaseA.do2(IsB()) // Ok, fails
The code below is found in scastie.
TIA
class BaseToCompare
class IsA extends BaseToCompare
class IsB extends BaseToCompare
class IsC extends IsA
trait UseBase:
type TT <: BaseToCompare
def do1[T <: BaseToCompare](t:T): Unit
def do2(t:TT): Unit
object UseBaseA extends UseBase :
type TT = IsA
def do1[T <: IsA](t:T): Unit = ()
def do2(t:TT): Unit = ()
@main
def run: Unit =
println("Started")
UseBaseA.do1(IsA())
UseBaseA.do1(IsB()) // Not failing
UseBaseA.do1(IsC())
//UseBaseA.do1(1)
UseBaseA.do2(IsA())
//UseBaseA.do2(IsB()) // Ok, fails
UseBaseA.do2(IsC()) // How to get this to fail, only
// Nothing to do
println("Done")
()