扩展空特征时使用类型参数的目的是什么?
What is the purpose of using type parameters for an empty trait when extending it?
我最近遇到了类似于下面的代码。
我想问的问题是Second的类型参数(即第一个类型参数)被case扩展时指定的可能用途是什么类.
我没有看到使用 A
、Option[A]
和 Seq[A]
的任何特殊原因,因为扩展案例 类 通过 [= 缩小了类型参数的范围14=].
我是否遗漏了任何要点?
sealed trait Top[A, B]
sealed trait Second[A, B] extends Top[A, B]
case class ThirdA[A <: SomeTrait, B <: AnotherTrait](
returnType: Class[A],
relation: B
) extends Second[A, B]
case class ThirdB[A <: SomeTrait, B <: AnotherTrait](
returnType: Class[A],
relation: B
) extends Second[Option[A], B]
case class ThirdC[A <: SomeTrait, B <: AnotherTrait](
returnType: Class[A],
relation: B
) extends Second[Seq[A], B]
按照你的逻辑
I mean what would the type parameters A
, Option[A]
and Seq[A]
be trying to achieve(enforce) here? My understanding is that using Only A
is the same here as long as the Second
is empty.
这个standard GADT
// data Lam :: * -> * where
// Lift :: a -> Lam a -- ^ lifted value
// Pair :: Lam a -> Lam b -> Lam (a, b) -- ^ product
// Lam :: (Lam a -> Lam b) -> Lam (a -> b) -- ^ lambda abstraction
// App :: Lam (a -> b) -> Lam a -> Lam b -- ^ function application
// Fix :: Lam (a -> a) -> Lam a -- ^ fixed point
sealed trait Lam[A]
case class Lift[A](a: A) extends Lam[A]
case class Pair[A, B](la: Lam[A], lb: Lam[B]) extends Lam[(A, B)]
case class LamC[A, B](f: Lam[A] => Lam[B]) extends Lam[A => B]
case class App[A, B](f: Lam[A => B], la: Lam[A]) extends Lam[B]
case class Fix[A](f: Lam[A => A]) extends Lam[A]
没有意义,因为Lift[A]
可以作为new Lam[A] {}
,Pair[A, B]
作为new Lam[(A, B)] {}
,LamC[A, B]
作为new Lam[A => B]
, App[A, B]
与 new Lam[B] {}
,Fix[A]
与 new Lam[A] {}
,而且 App[X, A]
与 Fix[A]
相同 :)
嗯,是的,但是那些类型的对象可以首先获得具有不同的对象。对于 Lam[A]
你需要一个 A
,对于 Pair[A, B]
你需要一个 Lam[A]
和 Lam[B]
等等
同样,是的,ThirdB[A, B]
是 ThirdA[Option[A], B]
,ThirdC
是 ThirdA[Seq[A], B]
。但是要拥有这些类型的对象,您首先需要拥有不同的对象。为了拥有 ThirdA[A, B]
,您需要类型为 Class[A]
和 B
的对象,为了拥有 ThirdB[A, B]
到 ThirdA[Option[A], B]
,您需要类型为 [=42= 的对象] 和 B
但你可以通过 Class[A]
和 B
直接拥有 ThirdB[A, B]
,为了让 ThirdC[A, B]
到 ThirdA[Seq[A], B]
你需要对象输入 Class[Seq[A]]
和 B
但你可以直接通过 Class[A]
和 B
得到 ThirdC[A, B]
。
例如,您可以匹配 ThirdA
、ThirdB
、ThirdC
并根据类型编写一些逻辑。因此,您可以为 Option
和 Seq
指定您的一般逻辑。
所以实际上这取决于 SomeTrait
、AnotherTrait
、Class
是什么。
我最近遇到了类似于下面的代码。
我想问的问题是Second的类型参数(即第一个类型参数)被case扩展时指定的可能用途是什么类.
我没有看到使用 A
、Option[A]
和 Seq[A]
的任何特殊原因,因为扩展案例 类 通过 [= 缩小了类型参数的范围14=].
我是否遗漏了任何要点?
sealed trait Top[A, B]
sealed trait Second[A, B] extends Top[A, B]
case class ThirdA[A <: SomeTrait, B <: AnotherTrait](
returnType: Class[A],
relation: B
) extends Second[A, B]
case class ThirdB[A <: SomeTrait, B <: AnotherTrait](
returnType: Class[A],
relation: B
) extends Second[Option[A], B]
case class ThirdC[A <: SomeTrait, B <: AnotherTrait](
returnType: Class[A],
relation: B
) extends Second[Seq[A], B]
按照你的逻辑
I mean what would the type parameters
A
,Option[A]
andSeq[A]
be trying to achieve(enforce) here? My understanding is that using OnlyA
is the same here as long as theSecond
is empty.
这个standard GADT
// data Lam :: * -> * where
// Lift :: a -> Lam a -- ^ lifted value
// Pair :: Lam a -> Lam b -> Lam (a, b) -- ^ product
// Lam :: (Lam a -> Lam b) -> Lam (a -> b) -- ^ lambda abstraction
// App :: Lam (a -> b) -> Lam a -> Lam b -- ^ function application
// Fix :: Lam (a -> a) -> Lam a -- ^ fixed point
sealed trait Lam[A]
case class Lift[A](a: A) extends Lam[A]
case class Pair[A, B](la: Lam[A], lb: Lam[B]) extends Lam[(A, B)]
case class LamC[A, B](f: Lam[A] => Lam[B]) extends Lam[A => B]
case class App[A, B](f: Lam[A => B], la: Lam[A]) extends Lam[B]
case class Fix[A](f: Lam[A => A]) extends Lam[A]
没有意义,因为Lift[A]
可以作为new Lam[A] {}
,Pair[A, B]
作为new Lam[(A, B)] {}
,LamC[A, B]
作为new Lam[A => B]
, App[A, B]
与 new Lam[B] {}
,Fix[A]
与 new Lam[A] {}
,而且 App[X, A]
与 Fix[A]
相同 :)
嗯,是的,但是那些类型的对象可以首先获得具有不同的对象。对于 Lam[A]
你需要一个 A
,对于 Pair[A, B]
你需要一个 Lam[A]
和 Lam[B]
等等
同样,是的,ThirdB[A, B]
是 ThirdA[Option[A], B]
,ThirdC
是 ThirdA[Seq[A], B]
。但是要拥有这些类型的对象,您首先需要拥有不同的对象。为了拥有 ThirdA[A, B]
,您需要类型为 Class[A]
和 B
的对象,为了拥有 ThirdB[A, B]
到 ThirdA[Option[A], B]
,您需要类型为 [=42= 的对象] 和 B
但你可以通过 Class[A]
和 B
直接拥有 ThirdB[A, B]
,为了让 ThirdC[A, B]
到 ThirdA[Seq[A], B]
你需要对象输入 Class[Seq[A]]
和 B
但你可以直接通过 Class[A]
和 B
得到 ThirdC[A, B]
。
例如,您可以匹配 ThirdA
、ThirdB
、ThirdC
并根据类型编写一些逻辑。因此,您可以为 Option
和 Seq
指定您的一般逻辑。
所以实际上这取决于 SomeTrait
、AnotherTrait
、Class
是什么。