gpt4 book ai didi

scala - 用简单的界限表达类型不等式条件

转载 作者:行者123 更新时间:2023-12-01 12:40:31 24 4
gpt4 key购买 nike

import shapeless._

trait Something[T <: Something[T, R], R] {}
class Test[T <: Something[T, R], R, T1 <: Something[T1, _] <:!< T](t: T, t1: T1) {}

但我得到:
type arguments [T1,?] do not conform to trait Something's type parameter bounds [T <: Something[T,R],R]

这是有道理的,除了我希望这能奏效:
class Test1[T <: Something[T, R], R, T1 <: Something[T1, R1] <:!< T, R1](t: T, t1: T1)

但它在 T1 <: Something[T, R] 上请求相同的界限.

我想说的是这个类有 4 个类型参数,每对描述 Something[T <: Something[T, R], R] 的不同后代.简单地说,强制执行 T != T1 .

这样做的正确方法是什么?

最佳答案

一个普遍的例子

import shapeless._
class Foo[A, B](a: A, b: B)(implicit ev: A =:!= B)
val x = new Foo(1, "hello")
// x: Foo[Int,String] = Foo@4015d0b9

val y = new Foo(1, 2)
// error: ambiguous implicit values:
// both method neqAmbig1 in package shapeless of type [A]=> shapeless.=:!=[A,A]
// and method neqAmbig2 in package shapeless of type [A]=> shapeless.=:!=[A,A]
// match expected type shapeless.=:!=[Int,Int]
// new Foo(1, 2)
// ^

在您的具体情况下
class Test[A, B, T <: Something[T, A], T1 <: Something[T1, B]](t: T, t1: T1)(implicit ev: T =:!= T1)

关于scala - 用简单的界限表达类型不等式条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25297017/

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