This works, but isn't really what I want: ```clas...
# announcements
j
This works, but isn't really what I want:
Copy code
class PhaseDefinition<C: GameContextBase, T: GamePhaseBase<C>>(val init: PhaseDefinition<C, T>.() -> Unit) {...}
r
What's wrong with it?
j
It puts the definition at the wrong place. If I have a class that looks like this:
Copy code
class Start(gameContext: GameContext) : GamePhaseBase<GameContext>(gameContext)
That second type is already defined (this is
C
in my class signature above). But I don't want to have to redefine it when constructing it later.
Copy code
inline fun <C: GameContextBase, reified T: GamePhaseBase<C>> phase(noinline init: PhaseDefinition<C, T>.() -> Unit): PhaseDefinition<C, T> {
    return PhaseDefinition(init)
}
I should add this is all in service of a DSL I'm trying to create. So instead of
phase<Start> {...}
, now I would need to write
phase<GameContext, Start> {...}
(with
GameContext
needing to be added to every call). I mean, it's certainly doable, just feels very messy.
And since
Start()
already knows the generic class on it, it seems weird that I need add it.
So I guess it isn't the class signature, now that I think about it, it's the function. I want that function to be smarter.
r
Gotcha, I run into that same problem from time to time. Don't really know another way to do that though, sadly
j
Yeah, that's what I am afraid of...
Oh, I think I've got it!
Copy code
inline fun <reified T: GamePhaseBase<out GameContextBase>> phase(noinline init: PhaseDefinition<out GameContextBase, T>.() -> Unit): PhaseDefinition<out GameContextBase, T> {
    return PhaseDefinition(init)
}

class PhaseDefinition<C: GameContextBase, T: GamePhaseBase<out C>>(val init: PhaseDefinition<C, T>.() -> Unit) {
}
I can be more specific since I know what base types should be allowed when using the helper method. That seems to have fixed the issue.
I was being too generic 😉