Git Product home page Git Product logo

Comments (3)

MateuszKubuszok avatar MateuszKubuszok commented on July 18, 2024 1

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 implicit. On Scala 3 that could be something with a Mirror I guess?

// 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 AnyVals for wrapping/unwrapping/rewrapping where we would only have to drop AnyVal check, but it haven't been done since:

  • it would be a change of behavior that nobody requested before
  • if any 1-value product is treated the same way as AnyVal I can imagine that in some cases like A1(B1(C1(D1(...)))) into A2(B2(C2(D2(...)))) testing every possibility (nesting or not) might explode the compilation time

so I was waiting till some sane way of saying what can be unwrapped besides AnyVals can be invented.

from chimney.

pomadchin avatar pomadchin commented on July 18, 2024

Transforming enums into underlying singletons i.e. via getValue could be extremely convenient, wondering are there any easy solutions to that right now?

from chimney.

pomadchin avatar pomadchin commented on July 18, 2024

@MateuszKubuszok Oh that's right! Thanks for pointers. I'll try to play around for the local solution, nice.

from chimney.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.