Chris B
08/12/2024, 8:01 AMChris B
08/12/2024, 8:05 AMPuzzle
. A Puzzle
contains a PuzzleState
. A PuzzleState
contains a Selection
and a Guess
. All of these are represented by subclasses, so a Crossword
might inherent from Puzzle
, with a CrosswordState
that consists of a GridSelection
and a GridGuess
.Chris B
08/12/2024, 8:10 AMclass CrosswordState : PuzzleState<GridSelection, GridGuess>()
I can't actually pass it into anything expecting a PuzzleState
.Sam
08/12/2024, 8:18 AMB
is a subtype of A
, that doesn't necessarily mean Foo<B>
is a subtype of Foo<A>
. Sounds like you might need to add some type variance.Sam
08/12/2024, 8:18 AMChris B
08/12/2024, 8:28 AMPuzzle<T: PuzzleState<Any>
can I define a method on Puzzle
which returns the specific Any
type on the PuzzleState
?Sam
08/12/2024, 8:30 AMclass Puzzle<S, T: PuzzleState<S>> {
val state: T
fun getStateSomething(): S = state.something
}
Chris B
08/12/2024, 8:31 AMPuzzle
as Puzzle<String, PuzzleState<String>>
, right?Chris B
08/12/2024, 8:32 AMPuzzleState<Puzzle<String>>
Sam
08/12/2024, 8:33 AMclass Puzzle<S> {
val state: PuzzleState<S>
fun getStateSomething(): S = state.something
}
but sometimes you do end up with a lot of repetition in generic types, it's trueChris B
08/12/2024, 8:37 AMChris B
08/12/2024, 8:38 AMChris B
08/12/2024, 8:39 AMSam
08/12/2024, 8:40 AM*
).Chris B
08/12/2024, 8:45 AMPuzzleState<T>
has a fun getData() : T
method, I'd like to be able to define a getData()
method on the Puzzle
which returns a String
or an Int
or whatever based on the type on the PuzzleState
Chris B
08/12/2024, 8:45 AMJoffrey
08/12/2024, 8:50 AMPuzzle
needs to get a specific type via Puzzle.getData
, then you actually need Puzzle
to know about that specific type. This "requirement" of yours is what creates the need for Puzzle
to know about nested stuffJoffrey
08/12/2024, 8:52 AMPuzzleState
type anywhere, in which case you might just need Puzzle
to know about the guess and the selection type:
open class Puzzle<S, G> {
protected val state: PuzzleState<S, G>
}
Chris B
08/12/2024, 8:52 AMDaniel Pitts
08/12/2024, 2:29 PMChris B
08/12/2024, 2:30 PMChris B
08/12/2024, 2:30 PMDaniel Pitts
08/12/2024, 2:30 PMChris B
08/12/2024, 2:34 PMChris B
08/12/2024, 2:35 PMDaniel Pitts
08/12/2024, 2:38 PMinterface Puzzle {
val state: PuzzleState
}
interface PuzzleState
class CrosswordPuzzle : Puzzle {
override val state: CrosswordPuzzleState = CrosswordPuzzleState()
}
class CrosswordPuzzleState : PuzzleState
No generics required here.Chris B
08/12/2024, 2:38 PMChris B
08/12/2024, 2:40 PMvar
Chris B
08/12/2024, 2:40 PMDaniel Pitts
08/12/2024, 2:41 PMvar
in CrosswordPuzzle, but Puzzle should only have it a val.Daniel Pitts
08/12/2024, 2:41 PMChris B
08/12/2024, 2:41 PMChris B
08/12/2024, 2:42 PMDaniel Pitts
08/12/2024, 2:42 PMChris B
08/12/2024, 2:43 PMDaniel Pitts
08/12/2024, 2:44 PMChris B
08/12/2024, 2:44 PMupdate
method on the ViewModels that replaces them.Daniel Pitts
08/12/2024, 2:45 PMChris B
08/12/2024, 2:47 PMDaniel Pitts
08/12/2024, 2:47 PMChris B
08/12/2024, 2:48 PMDaniel Pitts
08/12/2024, 2:48 PMChris B
08/12/2024, 2:49 PMDaniel Pitts
08/12/2024, 2:50 PMChris B
08/12/2024, 2:50 PMDaniel Pitts
08/12/2024, 2:53 PMChris B
08/12/2024, 2:54 PMDaniel Pitts
08/12/2024, 2:57 PMDaniel Pitts
08/12/2024, 2:57 PMDaniel Pitts
08/12/2024, 2:59 PM()->Unit
lambdas) that call methods directly on the known puzzle type. the subtype of puzzle would have methods that update the state depending on the action.Daniel Pitts
08/12/2024, 3:03 PMfun interface Choice {
fun chosen()
}
interface Puzzle {
val availableChoices: List<Choice>
}
class CrosswordPuzzle : Puzzle {
private var state: CrosswordPuzzleState = CrosswordPuzzleState("initial state")
override val availableChoices: List<Choice>
get() = listOf(
Choice { updateState("user chose first guess") },
Choice { updateState("user chose second guess") },
)
private fun updateState(newState: String) {
state = CrosswordPuzzleState(newState)
}
}
class CrosswordPuzzleState(val state: String)
Chris B
08/12/2024, 3:04 PMDaniel Pitts
08/12/2024, 3:05 PMChris B
08/12/2024, 3:09 PM