Amir Eldor
09/23/2020, 8:14 PMnanodeath
09/23/2020, 8:34 PMHowever, the developer can still create classes of Ship outside the factory.is this a feature or a bug
Amir Eldor
09/23/2020, 8:42 PMinternal
keyword, but maybe I'm barking at the wrong treenanodeath
09/23/2020, 8:49 PMclass Ship internal constructor(...)
should be sufficient, then making ShipFactory.newShip(...)
publicShip
itself accept Game
as a constructor argument that Ship registers with in the constructor, but...passing this
to other objects from a constructor is problematic from various perspectives (security, exception-handling)Game.addEntity(ship)
or it ain't gonna work"Amir Eldor
09/23/2020, 8:53 PMa.stuff.game
, but when I created a Ship instance in a.stuff.somethingelse
the compiler did not complain. Is this expected as the a.stuff
is common?nanodeath
09/23/2020, 8:55 PMinternal
works pretty differently from package private
-- internal
is accessible anywhere in the entire "compilation unit", e.g. your library/engineAmir Eldor
09/23/2020, 8:57 PM— any client inside this module who sees the declaring class sees itsinternal
members;internal
nanodeath
09/23/2020, 8:57 PMAmir Eldor
09/23/2020, 8:57 PMnanodeath
09/23/2020, 8:57 PMAmir Eldor
09/23/2020, 8:59 PMthis
is incompletenanodeath
09/23/2020, 9:01 PMAmir Eldor
09/23/2020, 9:01 PMnanodeath
09/23/2020, 9:02 PMAmir Eldor
09/23/2020, 9:04 PMVampire
09/23/2020, 10:49 PMnanodeath
09/23/2020, 11:40 PMprivate
to be way stricter than Java's private
when it comes to inner classes and the likeVampire
09/24/2020, 9:32 AMAmir Eldor
09/24/2020, 4:10 PMGame
? so every call to companion's buildShip could reference that Game I initially gave?Vampire
09/24/2020, 5:17 PMAmir Eldor
09/24/2020, 7:16 PMVampire
09/24/2020, 7:36 PMkt
class Ship private constructor() : Tickable {
companion object {
lateinit val game: Game
fun create() = Ship().also(game::register)
}
override fun tick() {
println("tock")
}
}
...
Ship.game = Game()
Ship.create()
Amir Eldor
09/24/2020, 7:41 PMVampire
09/24/2020, 7:54 PMYeah, that works too. I had something in mind like (untested written on mobile)
```kt
class Ship private constructor() : Tickable {
companion object {
fun producer(game: Game) : () -> Ship = {
Ship().also(game::register)
}
}
override fun tick() {
println("tock")
}
}
...
Ship.game = Game()
Ship.create()
Amir Eldor
09/25/2020, 12:10 AM