代码之家  ›  专栏  ›  技术社区  ›  Victor Basso

在shapless中何时需要依赖类型?

  •  2
  • Victor Basso  · 技术社区  · 6 年前

    据我所知,依赖类型允许您保留未指定的输出类型:

    例如,如果您有一个类型类:

    trait Last[In] {
      type Out
    }
    

    然后,可以在未指定输出类型的情况下调用实例:

    implicitly(Last[String :: Int :: HNil]) // output type calculated as Int
    

    AUX模式允许您再次指定输出类型:

    implicitly(Last.Aux[String :: Int :: HNil, Int])
    

    在隐式参数列表中需要它来处理输出类型( to work around a Scala limitation on dependent types )中。

    但是,如果您总是需要指定(或指定一个类型参数)输出类型,为什么要首先使用依赖类型(然后是aux)?

    我试着复制 Last 类型类来自shapelize'src,替换 type Out 在trait中添加一个type param并删除aux。它仍然有效。

    当我真的需要它们的时候是什么情况?

    1 回复  |  直到 6 年前
        1
  •  4
  •   Dmytro Mitin    6 年前

    我明白了 Sum[A, B] Sum[A, B] { type Out = C } Sum.Aux[A, B, C] 是的。我在问为什么我需要打字 Out 而不是 只是 Sum[A, B, C] 是的。

    区别在于部分应用。为了 trait MyTrait { type A; type B; type C } 您可以指定某些类型,而不指定其他类型(希望编译器推断它们)。但是为了 trait MyTrait[A, B, C] 您只能指定全部或不指定其中任何一个。 为了 Sum[A, B] { type Out } 您希望指定 A 我是说, B 没有具体说明 出局 (期望编译器基于引用现有的范围推断其值)。类似地 trait Last[In] { type Out } 您希望指定 In 没有具体说明 出局 (需要编译器推断其值)。 所以类型参数更像输入,类型成员更像输出。

    https://www.youtube.com/watch?v=R8GksuRw3VI

    Abstract types versus type parameters 以及相关的问题


    但具体什么时候,我想说 没有具体说明 出局 是吗?

    让我们考虑下面的例子。这是一个自然数加法的类型类:

    sealed trait Nat
    case object Zero extends Nat
    type Zero = Zero.type
    case class Succ[N <: Nat](n: N) extends Nat
    
    type One = Succ[Zero]
    type Two = Succ[One]
    type Three = Succ[Two]
    type Four = Succ[Three]
    type Five = Succ[Four]
    
    val one: One = Succ(Zero)
    val two: Two = Succ(one)
    val three: Three = Succ(two)
    val four: Four = Succ(three)
    val five: Five = Succ(four)
    
    trait Add[N <: Nat, M <: Nat] {
      type Out <: Nat
      def apply(n: N, m: M): Out
    }
    
    object Add {
      type Aux[N <: Nat, M <: Nat, Out0 <: Nat] = Add[N, M] { type Out = Out0 }
      def instance[N <: Nat, M <: Nat, Out0 <: Nat](f: (N, M) => Out0): Aux[N, M, Out0] = new Add[N, M] {
        override type Out = Out0
        override def apply(n: N, m: M): Out = f(n, m)
      }
    
      implicit def zeroAdd[M <: Nat]: Aux[Zero, M, M] = instance((_, m) => m)
      implicit def succAdd[N <: Nat, M <: Nat, N_addM <: Nat](implicit add: Aux[N, M, N_addM]): Aux[Succ[N], M, Succ[N_addM]] =
        instance((succN, m) => Succ(add(succN.n, m)))
    }
    

    这个类型类在类型级别上都有效

    implicitly[Add.Aux[Two, Three, Five]]
    

    和价值水平

    println(implicitly[Add[Two, Three]].apply(two, three))//Succ(Succ(Succ(Succ(Succ(Zero)))))
    assert(implicitly[Add[Two, Three]].apply(two, three) == five)//ok
    

    现在让我们用类型参数而不是类型成员重写它:

    trait Add[N <: Nat, M <: Nat, Out <: Nat] {
      def apply(n: N, m: M): Out
    }
    
    object Add {
      implicit def zeroAdd[M <: Nat]: Add[Zero, M, M] = (_, m) => m
      implicit def succAdd[N <: Nat, M <: Nat, N_addM <: Nat](implicit add: Add[N, M, N_addM]): Add[Succ[N], M, Succ[N_addM]] =
        (succN, m) => Succ(add(succN.n, m))
    }
    

    在类型级别上,它的工作原理类似

    implicitly[Add[Two, Three, Five]]
    

    但在值级别上,现在必须指定类型 Five 而在前一种情况下,它是由编译器推断的。

    println(implicitly[Add[Two, Three, Five]].apply(two, three))//Succ(Succ(Succ(Succ(Succ(Zero)))))
    assert(implicitly[Add[Two, Three, Five]].apply(two, three) == five)//ok
    

    所以区别在于部分应用。


    但是如果你加上 + 语法糖,就像你通常做的那样 实用型(无形的也适用于一切事物),依赖型 似乎无关紧要

    语法并不总是有用的。例如,让我们考虑一个类型类,它接受一个类型(但不接受此类型的值),并生成此类型的类型和值:

    trait MyTrait {
      type T
    }
    
    object Object1 extends MyTrait
    object Object2 extends MyTrait
    
    trait TypeClass[In] {
      type Out
      def apply(): Out
    }
    
    object TypeClass {
      type Aux[In, Out0] = TypeClass[In] { type Out = Out0 }
      def instance[In, Out0](x: Out0): Aux[In, Out0] = new TypeClass[In] {
        override type Out = Out0
        override def apply(): Out = x
      }
    
      def apply[In](implicit tc: TypeClass[In]): Aux[In, tc.Out] = tc
    
      implicit val makeInstance1: Aux[Object1.T, Int] = instance(1)
      implicit val makeInstance2: Aux[Object2.T, String] = instance("a")
    }
    
    println(TypeClass[Object1.T].apply())//1
    println(TypeClass[Object2.T].apply())//a
    

    但是如果我们 出局 一个类型参数,然后在调用时我们必须指定 出局 无法定义扩展方法和推断类型参数 从元素类型,因为没有类型的元素 Object1.T ,请 Object2.T 是的。