在Scala中,我可以将通用参数限制为超类型的子集吗?
sealed trait A
class X extends A
class Y extends A
class Z extends A
def myStrictFunc[T is X or Y, but not Z](o: T)
----------+----------
|
Can I have this constraint in Scala ?
答案 0 :(得分:1)
是的,你可以。例如,使用Shapeless
import shapeless.=:!=
sealed trait A
class X extends A
class Y extends A
class Z extends A
def myStrictFunc[T <: A](o: T)(implicit ev: T =:!= Z) = ???
否则您可以手动实施=:!=
:
答案 1 :(得分:1)
driver = webdriver.PhantomJS(executable_path='Complete path/to/phantomjs.exe')
sealed trait A {val id: Int}
class W extends A {val id = 13}
class X extends A {val id = 17}
class Y extends A {val id = 18}
class Z extends A {val id = 21}
trait Contra[-X]
type Union[A,B] = {
type Check[Z] = Contra[Contra[Z]] <:< Contra[Contra[A] with Contra[B]]
}
def myStrictFunc[T <: A : Union[X,Y]#Check](t: T): Int = t.id
myStrictFunc(new X) //res0: Int = 17
myStrictFunc(new Y) //res1: Int = 18
myStrictFunc(new Z) //won't compile
myStrictFunc(new W) //won't compile
类型可以扩展为3,4或更多类型,但代码有点冗长。
答案 2 :(得分:1)
使用Shapeless可能是目前最简单的方法,但是,您可能需要检查Dotty,它是下一代Scala编译器并具有built-in Union types。