-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
Transform into singleton types #407
Comments
Transforming enums into underlying singletons i.e. via getValue could be extremely convenient, wondering are there any easy solutions to that right now? |
I think something like that is cooked up by https://github.com/moia-oss/teleproto for PB enums. I'd say that some particular cases could be user-extended by providing the right generic // maybe it would work, haven't checked
given unwrapSingleton[A <: Product, B](
using Mirror.ProductOf[A] { type MirroredElemLabels = B *: EmptyTuple }
): Transformer[A, B] =
(src: A) => src.productIterator.head.asInstanceOf[B]
given wrapSingleton[A <: Product, B](
using m: Mirror.ProductOf[A] { type MirroredElemLabels = B *: EmptyTuple }
): Transformer[B, A] =
(src: B) => m.fromTuple(src *: EmptyTuple) If your enum extends some particular interface it can probably use it to extract the inner values. Technically, macro are not far from being able to always unwrap/wrap values - there are rules that handle
so I was waiting till some sane way of saying what can be unwrapped besides |
@MateuszKubuszok Oh that's right! Thanks for pointers. I'll try to play around for the local solution, nice. |
Released in 1.3.0 |
Currently the only kinds support for singleton types we have are:
case object
s (Scala 2 & 3) and parametherlessenum
case
s (Scala 3)Unit
as constructor parameter/setterNone
as constructor parameter/setter if we enable it with a flagenableOptionDefaultsToNone
We could allow transformting into/filling automatically any literal singleton types (e.g.
String
s,Int
s, etc literal types), since there is only 1 possible value that can be filled and we know exactly what's that value is.Implementation would require:
None
in some methods, so perhaps we should ignore some singleton types or only support primitives literals: Unit/String/Int/numerical types)The text was updated successfully, but these errors were encountered: