What is Nostr?
Anthony Accioly
npub1a6w…0tyc
2025-03-17 14:06:12
in reply to nevent1q…fnnl

Anthony Accioly on Nostr: I don't think that there's a way to override it since enums are sealed classes (but I ...

I don't think that there's a way to override it since enums are sealed classes (but I may be wrong). You can add your own method to a companion Object through

object MyEnum {
def fromString(name: String): Option[MyEnum] =
scala.util.Try(Color.valueOf(MyEnum)).toOption
}

From there you can probably use some fancy type system magic to add fromString to all subtypes of scala.reflect.Enum (warning, untested)

extension [T <: Enum](companion: T Companion) {
def fromString(name: String): Option[T] =
scala.util.Try(companion.valueOf(name)).toOption
}
Author Public Key
npub1a6we08n7zsv2na689whc9hykpq4q6sj3kaauk9c2dm8vj0adlajq7w0tyc