Understanding the Aux pattern in Scala Type System

ぐ巨炮叔叔 提交于 2020-01-20 04:34:25

问题


This question may be asked and answered before, but I would like to understand this with an example and I could not reason out where the Aux pattern might be helpful! So here is the trait:

trait Foo[A] {
  type B
  def value: B
}

Why do I have a type that is kind of bound to the return type of the value function? What do I achieve doing this? In particular, where would I use such patterns?


回答1:


Imagine a typeclass for getting the last element of any tuple.

trait Last[A] {
  type B
  def last(a: A): B
}

object Last {
  type Aux[A,B0] = Last[A] { type B = B0 }

  implicit def tuple1Last[A]: Aux[Tuple1[A],A] = new Last[Tuple1[A]] {
    type B = A
    def last(a: Tuple1[A]) = a._1
  }

  implicit def tuple2Last[A,C]: Aux[(A,C),C] = new Last[(A,C)] {
    type B = C
    def last(a: (A,C)) = a._2
  }

  ...
}

The type B always depends on the type A, that's why A is an input type of the typeclass and B is an output type.

Now if you want a function that can sort any list of tuples based on the last element you need access to the B type in the same argument list. That's the main reason, in the current state of Scala, why you need the Aux pattern: currently it's not possible to refer to the last.B type in the same parameter list as where last is defined, nor is it possible to have multiple implicit parameter lists.

def sort[A,B](as: List[A])(implicit last: Last.Aux[A,B], ord: Ordering[B]) = as.sortBy(last.last)

Of course you can always write Last[A] { type B = B0 } out in full, but obviously that becomes very impractical very quickly (imagine adding a couple more implicit parameters with dependent types, something that's very common with Shapeless); that's where the Aux type alias comes in.



来源:https://stackoverflow.com/questions/43900674/understanding-the-aux-pattern-in-scala-type-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!