tkroman
tkroman

Reputation: 4798

How to get a single defined element of a Coproduct instance?

I'll be concise:

val pvSchema = RecordType.like(
  's  ->> "" ::
  'l  ->> Long.MaxValue ::
  HNil
)

type PossibleValue = pvSchema.Union

val v = Coproduct[PossibleValue]('s -> "string")

To get Some[String] I could use v.get('s), while v.get('i) returns me None. Can I somehow do v.unconditionalGet() to get the defined value without checking for all other possibilities?

Upvotes: 1

Views: 109

Answers (1)

tkroman
tkroman

Reputation: 4798

Turns out that my question was a bit incorrect to start with. Since we deal with the union type, getting some result without knowing it's type beforehand becomes kind of a nonsense: one can't use that result safely. E.g.:

// suppose this is what I want
val value = v.getPresent()

use(value)

Here use should be able to handle both String and Int, and least upper bound for that is something like Any, and that means no desired type safety again.

I've somewhat reconsidered my design and everything works fine now.

Upvotes: 2

Related Questions