You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Should we allow implicit on enumcases? As @milessabin has stated, we could use an enum as a shapeless Poly with implicit cases corresponding to the Case's of the Poly.
For example, we have the following typelevel function that computes the index of a type in an HList:
traitIndexOf[A, U] extendsDepFn0 {
typeOut<:NatdeftoInt:Int
}
objectIndexOf {
defapply[A, U](implicito: IndexOf[A, U]):Aux[A, U, o.Out] = o
typeAux[A, U, I<:Nat] =IndexOf[A, U] { typeOut=I }
implicitdefcase0[At<:HList, U]:Aux[U::At, U, _0] =newIndexOf[U::At, U] {
typeOut= _0
defapply() =Nat._0
deftoInt=0
}
implicitdefcaseN[At<:HList, Ah, U, I<:Nat]
(implicitp: IndexOf.Aux[At, U, I]):Aux[Ah::At, U, Succ[I]] =newIndexOf[Ah::At, U] {
typeOut=Succ[I]
defapply() =Succ[I]()
deftoInt= p.toInt +1
}
}
This is quite verbose. Using implicit cases, I hope that this could be simplified to something like this:
where each implicit case corresponds to a clause (as in programming in Prolog).
When desugaring, each case is turned into a class in the enum. Since implicit class in Scala is treated as a class and an implicit def (that converts an element to the implicit class wrapper), we could desugar each implicit case into a class and an implicit def. For example, the above could be desugared as:
@ctongfei I'm really not convinced given how verbose and cumbersome the suggested IndexOf with enum is. Also, it seems that you are trying to use enum for something completely new and unrelated to their original use case, which is to defined algebraic data types.
What you really want here is dependent types, which would allow you to write is something like the following:
defindexOf(e: Any, l: Hlist):Nat=
l match {
case x :: xs =>if (x == e) ZeroelseSucc(indexOf(e, xs))
caseHNil=>thrownewNoSuchElementException("...")
}
And have if run by typer in one way or another to infer the precise Nat that corresponds to the index of element e in l. With match types, you could translate the above function into a type:
(not so sure about the X == E part, but you get the idea)
With what @gsps and I have been working on in #4806 and the follow up PR you would be able to annotate the value level indexOf method as a dependent def and get the same result.
This is inspired by #5495 .
Should we allow
implicit
onenum
case
s? As @milessabin has stated, we could use an enum as a shapelessPoly
with implicit cases corresponding to theCase
's of thePoly
.For example, we have the following typelevel function that computes the index of a type in an
HList
:This is quite verbose. Using
implicit case
s, I hope that this could be simplified to something like this:where each
implicit case
corresponds to a clause (as in programming in Prolog).When desugaring, each
case
is turned into a class in theenum
. Sinceimplicit class
in Scala is treated as a class and animplicit def
(that converts an element to the implicit class wrapper), we could desugar eachimplicit case
into a class and animplicit def
. For example, the above could be desugared as:Or I wonder what match-types would enable us in this situation?
The text was updated successfully, but these errors were encountered: