More concise "Either"?

Er, if the following works:

val a = ContextA()
val instructions: FormParser.InstructionsContext = a.instructions()

And so does this:

val b = ContextB()
val instructions: FormParser.InstructionsContext = b.instructions()

Then you wouldn’t expect the following to work intrinsically, without FormParserContextScope?

val ab: magic union syntax between ContextA and ContextB = a ?: b
val instructions: FormParser.InstructionsContext = ab.instructions()

And if not, then isn’t magic union syntax between ContextA and ContextB just the common ancestor, same as today?

You’d expect error: unresolved reference. None of the following candidates is applicable because of receiver type mismatch, like today?

I guess denotable union types just means you can write, e.g. ContextA | ContextB vs. BaseContext. Except the former would preclude ContextC. Which would be most handy in cases like, e.g. Error | String. Sorry, thinking out loud.

In that case your (hypothetical) FormParserContextScope< magic union syntax between ContextA and ContextB > implementation makes sense, thanks!

It’s weird I find, having to write

when (this) {
  is ContextA: ranges()
  is ContextB: ranges()
}

but not

when (this) {
  is ContextA, is ContextB: ranges()
}

Subtle. I guess it makes sense if (without a common ancestor) ContextA::ranges()ContextB::ranges(). Will take some getting used to, but probably a rare case. Thanks again for all the help!

1 Like