Borders in Scala provide more granular control over types in Scala, such as argument types. for instance
def foo[ S <: String ]( arg: S ): S = ...
The above function allows you to accept arguments that are subtypes of String, also
def bar[ S >: A <: B ]( arg: S ): S = ...
The above allows you to set the upper and lower bounds so that S is a subtype of B and super-type A.
My question (I believe that restrictions are included) is whether it would be possible to set the argument type so that the argument is a supertype of say String, but does not include some supertype of the lower bound (in this case String) say type Any.
UPDATE
sealed trait Helper[S]
object Helper {
implicit def stringSubtype[S >: String] = new Helper[S]{}
implicit def any = new Helper[Any]{}
}
def foo[S: Helper](r: S): Int = 1
val arg1: String = "hi"
val arg2: Any = "hello"
foo(arg1)
foo(arg2)
I expect a call with arg2 to throw an illegal argument.
source
share