I have a simple use case, parsing JSON.
This is my typeclass, simplified:
trait JsonReader[T]:
def parse(jValue: String): T
So basically I want to convert a String to a simple enum:
enum Color:
case Red, Blue
I want to parse string “Red” to Color.Red enum.
The only thing I found that is close is ctx.choose(v), but that works on T values, and I only have a String…
Another thing that comes to my mind is using runtime reflection (for which I don’t have a working example).
But it would make sense to me that we can do this at compile time.
If you have access to the SealedTrait instance, you can get the subtypes via .subtypesArray, and then access the .typeName to find the appropriate subtype. From that, you can get the typeclass instance for the subtype.
You can then call .parse on that typeclass instance - which corresponds to the specific enum value. This should be an instance of a CaseClass. The rawConstruct with an empty field value should give you the desired instance.