gpt4 book ai didi

scala - Scala 中类型不相等的证据

转载 作者:行者123 更新时间:2023-12-04 22:20:30 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Enforce type difference

(7 个回答)


4年前关闭。




有没有办法约束一个方法,以便只有在两种类型被证明不相等时才有意义?

trait Something[A, B] {
// I can only be called if type A is the same as type B
def ifEqual(implicit ev: A =:= B)

// Now I cannot be called if type A is proven to be the same as type B
def ifNotEqual(implicit ev: A ??? B)
}

最佳答案

是的。来自 shapeless ,

// Type inequalities
trait =:!=[A, B]

implicit def neq[A, B] : A =:!= B = new =:!=[A, B] {}
implicit def neqAmbig1[A] : A =:!= A = ???
implicit def neqAmbig2[A] : A =:!= A = ???

关于scala - Scala 中类型不相等的证据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24519487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com