-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sound withFirst
and withOnly
alternatives to summonFirst
and summonOnly
#18
Conversation
Yeah I noticed that too but I think we should have |
On second thought I'm coming round to the idea of a |
Also, it turns out that signature isn't possible in Scala 3 with irreducible existential types being dropped |
Ah I guess that's the reason why there is a cast currently.
Interesting, I'm not sure what useful methods it would have. And what about |
IIRC |
The LUB only works when |
Exactly - and when it's invariant, as with Empty, there's no sound option. |
There is a sound option, it's just very constrained. |
Sorry yes - I meant no sound option without knowing the type for the specific instance. Something like this seems to work but I'm not sure if it justifies the extra complexity: object FirstCoproductInstance {
type Aux[F[_], T, U0] = FirstCoproductInstance[F[_], T] { type U = U0}
private transparent inline def derive[F[_], T, T0]: FirstCoproductInstance[F, T] = inline erasedValue[LiftP[F, T0]] match {
case _: (a *: b) => summonFrom {
case aa: `a` => new FirstCoproductInstance[F, T] {
type U = Head[T0]
val instance: F[U] = aa.asInstanceOf
}
case _ => derive[F, T, Tail[T0]]
}
}
transparent inline given[F[_], T](using CoproductGeneric[T]): FirstCoproductInstance[F, T] = derive[F, T, T]
}
def summonFirst[F[_], T](using sf: FirstCoproductInstance[F, T]): F[sf.U] = sf.instance |
probably just
Actually, I think we'd need that in |
I think we should just emulate an existential with something like trait Existential[F[_]] {
type A
def value: F[A]
} |
Why not just use |
summonFirst
with sound alternativewithFirst
and withOnly
alternatives to summonFirst
and summonOnly
I think users expect the public API of Shapeless to be type-safe, but
summonFirst
does a cast that isn't in general correct (e.g. it would happily widenExtract[Some]
toExtract[Option]
).I tried to write a sound version but I think it would require Shapeless 2 style typeclass-based dependent typing, so I'm not sure it's worth it.
This doesn't cover
K11
andK2
- I'll add them if this is approved in principle.