Ray Rahke
03/09/2024, 3:44 PMstatic
, and swift lacks abstract
... pick my poision I guess.Adam S
03/09/2024, 3:48 PMRay Rahke
03/09/2024, 5:17 PMRay Rahke
03/09/2024, 5:19 PMRay Rahke
03/09/2024, 5:19 PMRob Elliot
03/09/2024, 5:20 PM@JvmStatic
if you want it compiled to a static field / method on the JVM.Ray Rahke
03/09/2024, 5:20 PMRay Rahke
03/09/2024, 5:22 PMworth
property exists on all of the pieces, which means worth
is abstract.
2nd observation: The worth
property has to do with the types of pieces, and not the instances / dynamic state, which means worth
is staticRay Rahke
03/09/2024, 5:24 PMabstract class Piece {
abstract static worth: Int
}
class Rook : Piece {
static worth: Int = 4
}
class Pawn : Piece {
static worth: Int = 1
}
Ray Rahke
03/09/2024, 5:24 PMRob Elliot
03/09/2024, 5:25 PMRay Rahke
03/09/2024, 5:26 PMRob Elliot
03/09/2024, 5:51 PMenum class ChessPieceType(
val worth: Int
) {
PAWN(1),
ROOK(5), // etc
}
class ChessPiece(val type: ChessPieceType, val x: Int, val y: Int) {
val worth = type.worth
}
Ray Rahke
03/09/2024, 7:00 PMRay Rahke
03/09/2024, 7:01 PMRay Rahke
03/09/2024, 7:02 PMRob Elliot
03/09/2024, 7:02 PMRay Rahke
03/09/2024, 7:03 PMRay Rahke
03/09/2024, 7:04 PMRay Rahke
03/09/2024, 7:04 PMRay Rahke
03/09/2024, 7:04 PMRay Rahke
03/09/2024, 7:04 PMRay Rahke
03/09/2024, 7:05 PMRay Rahke
03/09/2024, 7:05 PMRay Rahke
03/09/2024, 7:05 PMis_legal_move(from, to): boolean
Rob Elliot
03/09/2024, 7:05 PMimagine each Piece sub type has their own polymoprhic implementation of a methodLike this.
enum class ChessPieceType(
val worth: Int?
) {
PAWN(1) {
override fun validMoves(piece: ChessPiece): List<ChessPiece> {
TODO("Not yet implemented")
}
},
ROOK(5) {
override fun validMoves(piece: ChessPiece): List<ChessPiece> {
TODO("Not yet implemented")
}
};
abstract fun validMoves(piece: ChessPiece): List<ChessPiece>
}
class ChessPiece(
val type: ChessPieceType,
val x: Int,
val y: Int,
) {
val worth = type.worth
fun validMoves(): List<ChessPiece> = type.validMoves(this)
}
Ray Rahke
03/09/2024, 7:06 PMRay Rahke
03/09/2024, 7:06 PMRay Rahke
03/09/2024, 7:07 PMRay Rahke
03/09/2024, 7:07 PMRay Rahke
03/09/2024, 7:07 PMRay Rahke
03/09/2024, 7:08 PMRay Rahke
03/09/2024, 7:08 PMRay Rahke
03/09/2024, 7:08 PMRay Rahke
03/09/2024, 7:09 PMnew(x,y)
method on each enum variantRay Rahke
03/09/2024, 7:09 PMx
,y
as propertiesRay Rahke
03/09/2024, 7:09 PMRay Rahke
03/09/2024, 7:12 PMRay Rahke
03/09/2024, 7:16 PMinterface PieceInstance {
val x: Int
val y: Int
}
enum Piece(worth: Int) {
Rook(4) {
fun new(x: Int, y: Int): PieceInstance {
return object : PieceInstance {
val x = x;
val y = y;
}
}
},
a worse version of classesArjan van Wieringen
03/10/2024, 11:20 AMStephan Schröder
03/10/2024, 12:34 PMokarm
03/11/2024, 12:36 PMokay if you can put methods on enums that is not as badThis quote, coupled with the nonsensical piece of code from the last message, indicates to me that Ray should have been directed to
sealed classes
, which is what he reinvented in his last message.Ray Rahke
03/12/2024, 4:53 PMabstract class Animal (val name: String, var health: Int) {
abstract static conflicts: List<KClass<Animal>>
// we may refuse to let a type of animal to exist in same ecosystem as another animal
}
class Cat (name: String, health: Int) : Animal(name, health) {
static conflicts: listOf(Dog)
}
class Dog (name: String, health: Int) : Animal(name, health) {
static conflicts: listOf(Cat)
}
class Ecosystem {
animals: List<Animal>
speciesfilter: List<KClass<Animal>> // we can restrict this ecosystem to certain animal types
fun no_conflicts(a: Animal, b: Animal) {
return (b::class !in a::class.conflicts && a::class !in b::class.conflicts)
}
fun add(animal: Animal) {
if (species.all { no_conflicts(it, animal) })
if (speciesfilter.all { it.isInstance(animal) } )
animals.add(animal)
else fail
}
}
Ray Rahke
03/12/2024, 4:54 PMRay Rahke
03/12/2024, 4:55 PMRay Rahke
03/12/2024, 4:58 PM