What does the Aux model do in Scala?

I have some sense of an Aux template (as used in formless and elsewhere) in which the type member is retrieved into the type parameter, and I know that the workaround is the fact that the arguments in the same argument list can't depend apart, but I don’t quite understand what he used and what problems he solves.

For example, I'm currently trying to figure out how to save and work with the more specific type returned by the whitebox macro - is this for Aux?

Is there a simple description?

+5
source share
1 answer

Simply put, this template allows you to establish a relationship between two parameters of a typical type.

Let's look at a shapeless class like LabelledGeneric , which gives you a general overview of the HList for case classes:

 trait LabelledGeneric[T] { type Repr } 

T is the type of input, i.e. LabelledGeneric[MyCaseClass] will provide you with an HList MyCaseClass . Repr is the output type, that is, the HList type corresponding to T

Suppose you write a method that accepts a Generic instance and needs another output type parameter. For example, we could use Keys to collect field names of a marked common

 def fieldNames[T](implicit gen: LabelledGeneric[T], keys: Keys[gen.Repr]): keys.Repr … 

Except this does not work because Scala does not allow you to access gen or Keys here. We can either have a specific type or a type variable.

And the fact that Aux plays the game: it allows you to "raise" gen.Repr into a variable of the type:

 object Generic { type Aux[T, Repr0] = Generic[T] { type Repr = Repr0 } } 

As you can see, the Aux type gives us the path from Repr to the type variable, so we can finally define foo :

 def foo[T, Repr, K]( implicit gen: LabelledGeneric.Aux[T, Repr], keys: Keys.Aux[Repr, K] ): K … 

If you are familiar with Prolog, you can read Aux as a predicate that proves the relationship between two type variables. In the above example, you can read it as "LabelledGeneric" proves that Repr is a generic representation with T labels, and Keys.Aux proves that K is a list of all Repr keys. "

+5
source

All Articles