News for hackers. Stuff that matters.
Re:How I Learned to Stop Worrying and Love the State Machine by bushin on Monday February 26, 2018 @07:57PM (#16470319)
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()
    }
Re:How I Learned to Stop Worrying and Love the State Machine by schoen on Monday February 26, 2018 @08:14PM (#16470405)
If 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.
Re:How I Learned to Stop Worrying and Love the State Machine by bushin on Monday February 26, 2018 @09:24PM (#16470669)
Well, 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
    }
Re:How I Learned to Stop Worrying and Love the State Machine by icebraining on Tuesday February 27, 2018 @02:56AM (#16471780)
Python 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()
Re:How I Learned to Stop Worrying and Love the State Machine by icebraining on Tuesday February 27, 2018 @02:50AM (#16471757)

    class Wizard extends Player {
      type T  = Dagger | Staff
      var weapon: T = null
    }
The billion-dollar mistake in a new language? What a waste.
Re:How I Learned to Stop Worrying and Love the State Machine by boubiyeah on Tuesday February 27, 2018 @03:59AM (#16472055)
Yeah that freaking blows. They keep it for compatibility with java. It's not idiomatic code though, you would use an Option.
Re:How I Learned to Stop Worrying and Love the State Machine by paganel on Tuesday February 27, 2018 @08:09AM (#16473027)
Until 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.
Re:How I Learned to Stop Worrying and Love the State Machine by yojex on Tuesday February 27, 2018 @08:34AM (#16473161)
Honest 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.
Re:How I Learned to Stop Worrying and Love the State Machine by bushin on Tuesday February 27, 2018 @11:23AM (#16474892)
You probably want to solve "the expression problem".
Re:How I Learned to Stop Worrying and Love the State Machine by cellularmitosis on Thursday March 01, 2018 @01:06PM (#16494471)
I'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 {}

Re:How I Learned to Stop Worrying and Love the State Machine by bushin on Tuesday February 27, 2018 @11:14AM (#16474781)
That's why protocol extensions were implemented.
Re:How I Learned to Stop Worrying and Love the State Machine by gameswithgo on Tuesday February 27, 2018 @12:48AM (#16471350)
That forces wizard to always have a staff and warrior to always have a sword?

warrior can also use a staff

both can use daggers