7 ms·
Shame that Swift is pretty much confined to iOS app development. protocol Weapon {} class Staff: Weapon {} class Sword: Weapon {} protocol Pla
by bushin 9y ago
Shame that Swift is pretty much confined to iOS app development.
protocol Weapon {}
class Staff: Weapon {}
class Sword: Weapon {}
protocol Player {
associatedtype T: Weapon
var weapon: T { get set }
}
class Wizard: Player {
var weapon = Staff()
}
class Warrior: Player {
var weapon = Sword()
}
- schoen 9y agoIf you look through the later items in the series, it seems to get more complex than this. In particular, a character can use either 0 or 1 weapons at a particular moment, and either class can also use the Dagger weapon. It's not that each character inherently only has, and uses, the only weapon appropriate to that character -- this would make things quite a bit simpler.
- bushin 9y agoWell, maybe someday Dotty will be a mainstream language... trait Weapon class Sword extends Weapon class Staff extends Weapon class Dagger extends Weapon trait Player { type T <: Weapon } class Wizard extends Player { type T = Dagger | Staff var weapon: T = null } class Warrior extends Player { type T = Dagger | Sword var weapon: T = new Sword }
- icebraining 9y agoclass Wizard extends Player { type T = Dagger | Staff var weapon: T = null } The billion-dollar mistake in a new language? What a waste.
- boubiyeah 9y agoYeah that freaking blows. They keep it for compatibility with java. It's not idiomatic code though, you would use an Option.
- icebraining 9y agoPython already is a mainstream language: class Weapon: pass class Sword(Weapon): pass class Staff(Weapon): pass class Dagger(Weapon): pass class Player: weapon: Weapon class Wizard(Player): weapon: Optional[Union[Dagger, Staff]] = None class Warrior(Player): weapon: Optional[Union[Dagger, Sword]] = Sword()
- gameswithgo 9y agoThat forces wizard to always have a staff and warrior to always have a sword? warrior can also use a staff both can use daggers
- paganel 9y agoUntil you want to model a bar-fight and then a chair (a piece of furniture) becomes a weapon, i.e. "class Chair: Furniture{}" turns into "Class Chair: Weapon {}". Or when the winter gets tougher and longer and "class Chair: Furniture{}" changes into "class Chair: Firewood{}". I know that there's probably a "solution" involving object "composition", but that would only mask the problem, will not really solve it.
- yojex 9y agoHonest question: does anyone have a clean solution to this problem? I've run into this often and always end up hacking something ugly to make it work.
- bushin 9y agoYou probably want to solve "the expression problem".
- bushin 9y agoThat's why protocol extensions were implemented.
- cellularmitosis 9y agoI'm not sure I understand your objection. Those are protocols, so Chair can adhere to all of them at once. class Chair: Furniture, Weapon, Firewood {}