Is there a more concise way to write the following, with less repetition? I.e. an interface for the common members of existing instances of two different classes.
FormParser.ParaContext
and FormParser.SepContext
are third-party classes (not interfaces), so I can extend them, but beyond that my options are limited, I think?
I was kinda hoping I wouldn’t have to enumerate the common members of a sealed class/interface. Or that I could delegate to a class. Or add an extension inner class.
sealed interface Either {
class ParaContext(val ctx: FormParser.ParaContext) : Either {
override fun ranges() = ctx.ranges()
override fun rest() = ctx.rest()
override fun SUBTOTAL() = ctx.SUBTOTAL()
override fun instructions() = ctx.instructions()
override val amount = ctx.amount
}
class SepContext(val ctx: FormParser.SepContext) : Either {
override fun ranges() = ctx.ranges()
override fun rest() = ctx.rest()
override fun SUBTOTAL() = ctx.SUBTOTAL()
override fun instructions() = ctx.instructions()
override val amount = ctx.amount
}
fun ranges(): FormParser.RangesContext?
fun rest(): FormParser.RestContext?
fun SUBTOTAL(): TerminalNode?
fun instructions(): FormParser.InstructionsContext
val amount: Token?
}
Ideally, I think I’d like the following to work:
sealed interface Either {
class ParaContext(ctx: FormParser.ParaContext) : FormParser.ParaContext by ctx, Either
class SepContext(ctx: FormParser.SepContext) : FormParser.SepContext by ctx, Either
}
fun evalLine(ctx: Either) {
val instructions = ctx.instructions()
[...]
But I’d settle for a suggestion that works and is less repetitious than what I have