# Instantiate enum from String

**URL:** https://softwaremill.community/t/instantiate-enum-from-string/111
**Category:** magnolia
**Created:** [February 3, 2023, 10:37am UTC](https://softwaremill.community/t/instantiate-enum-from-string/111 "2023-02-03T10:37:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sake92](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/sake92/32/85_2.png) [@sake92](https://softwaremill.community/u/sake92)
#### Post date: [February 3, 2023, 10:37am UTC](https://softwaremill.community/t/instantiate-enum-from-string/111/1 "2023-02-03T10:37:21Z")

</div>

I have a simple use case, parsing JSON.  
This is my typeclass, simplified:

```plaintext
trait JsonReader[T]:
  def parse(jValue: String): T

```

So basically I want to convert a String to a simple enum:

```plaintext
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.

Is there an easy way I can do this with magnolia?

---

<div class="post-metadata">

### Author: ![adamw](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/adamw/32/28_2.png) [@adamw](https://softwaremill.community/u/adamw)
#### Post date: [February 3, 2023, 3:36pm UTC](https://softwaremill.community/t/instantiate-enum-from-string/111/2 "2023-02-03T15:36:04Z")

</div>

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.

Let me know if this helps 🙂

---

<div class="post-metadata">

### Author: ![adamw](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/adamw/32/28_2.png) [@adamw](https://softwaremill.community/u/adamw)
#### Post date: [February 3, 2023, 3:48pm UTC](https://softwaremill.community/t/instantiate-enum-from-string/111/3 "2023-02-03T15:48:29Z")

</div>

Maybe the `Show` typeclass and this test will help: [magnolia/SumsTests.scala at f5df7e63ce41375871524f442d0438aba698c6b7 · softwaremill/magnolia · GitHub](https://github.com/softwaremill/magnolia/blob/f5df7e63ce41375871524f442d0438aba698c6b7/src/test/SumsTests.scala#L54)

---

<div class="post-metadata">

### Author: ![sake92](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/sake92/32/85_2.png) [@sake92](https://softwaremill.community/u/sake92)
#### Post date: [February 6, 2023, 7:04am UTC](https://softwaremill.community/t/instantiate-enum-from-string/111/4 "2023-02-06T07:04:02Z")

</div>

Thank you very much Adam!  
Seems like I was missing the `rawConstruct` and what it does… It’s really handy. 🙂

An example in Scastie if anyone needs it: [Scastie - An interactive playground for Scala.](https://scastie.scala-lang.org/MgUtg3gCQ6KtLzy40kSUMg)

```plaintext
import magnolia1.*

trait ParseString[T] {
  def parse(str: String): T
}

object ParseString extends AutoDerivation[ParseString]:
  def join[T](ctx: CaseClass[ParseString, T]): ParseString[T] = value =>
    ctx.rawConstruct(Seq())

  override def split[T](ctx: SealedTrait[Typeclass, T]): ParseString[T] = value =>
    val subtype = ctx.subtypes.find(_.typeInfo.short == value).get
    subtype.typeclass.parse(value)

/////
enum Color derives ParseString:
  case Red

val tc = summon[ParseString[Color]]
println( tc.parse("Red") )

```
