1

I have an inner case class, specifically an event from this question, and want to match it, including the outer object:

class Player {
  var _life = 20
  def life = _life

  def gainLife(life: Int) = execute(GainLife(life))

  case class GainLife(life: Int) extends Event {
    def execute() = _life += life
  }
}

I can easily write an effect (a partial function) that replaces life events for a specific player:

//gain twice as much life
def effect(player: Player): ReplacementEffect = {
  case player.GainLife(x) => player.GainLife(x * 2)
}

however, I can't do the same for other players. The closest I have come is this:

//only you gain life
def effect2(player: Player): ReplacementEffect = {
  case evt: Player#GainLife => player.GainLife(evt.life)
}

But 1) this replaces even your own lifegain with a new lifegain, 2) I can't reference the player that originally gained life in the function and 3) I'm missing out on directly matching life this way.

This could be expressed using a path-independent type like

case Player.GainLife(_player, life) if _player != player => GainLife(player, life)

Ideally, I want something like

case _player.GainLife(life) if _player != player => player.GainLife(life)

Is this possible somehow, or can I work around this? Or do I have to resort to making GainLife nested?

2 Answers 2

5

When you define the class inside of another it means that the type is specific to the surrounding class, so playerA.GainLife is not the same type as playerB.GainLife (this is called path dependent types) if you want it to mean the same thing you define it in a scope that is the same regardless of instance: the package or the companion object of your class.

You can read more in this question: What is meant by Scala's path-dependent types?

4
  • I think I see in which direction you're going, but it's not exactly what I want. I'm happy with GainLife depending on the affected Player, I just want to use that player while pattern matching. I have added what I think you're suggesting to the question, but that's what I'd like to avoid, because I think GainLife being path dependent is actually cleaner, and it would be the only reason to add a companion object. Feb 10, 2015 at 12:07
  • I have found a way that I find rather satisfying, would you mind suggesting some improvements if you have any? Feb 10, 2015 at 13:09
  • This all feels a bit weird to me, but maybe I just dont quite grok what you are trying to achieve. If path dependent types forces you create custom special unnapply I would probably just make a GainLife event that is not path dependent and just add all data that is needed to it rather than this. Guess it's a question of taste. Feb 10, 2015 at 13:17
  • I agree it's weird, but it's as close to a direct answer to my question as I got, so I wanted to share it nonetheless. There are some benefits, however: access to private[this] fields, no need to write player. in the execute() method, or this when creating the event. I feel my variant is a little wordy, but only in one location (the companion object). The client code, including the effects, gainLife() and execute(), look nicer to me. But I'm probably overthinking the importance here. Feb 10, 2015 at 15:57
0

The closest I have come is to define my own unapply method:

class Player {
  self =>

  var _life = 20
  def life = _life

  def gainLife(life: Int) = execute(GainLife(life))

  case class GainLife(life: Int) extends Event {
    def player = self

    def execute() = _life += life
  }
}

object Player {
  object _GainLife {
    def unapply(event: Player#GainLife) =
      Some((event.player, event.life))
  }
}

Note that naming the Player._GainLife object Player.GainLife instead would cause a name conflict, that is the most important downside here. Therefore, I chose to make that type available from outside the Player namespace:

val GainLife = Player._GainLife

This allows to match using both player.GainLife.unapply and Player._GainLife.unapply in a concise way:

//gain twice as much life
def effect1(player: Player): ReplacementEffect = {
  case player.GainLife(life) => player.GainLife(life * 2)
}

//only you gain life
def effect2(player: Player): ReplacementEffect = {
  case GainLife(_player, life) if _player != player => player.GainLife(life)
}

//all players gain twice as much life
def effect3: ReplacementEffect = {
  case GainLife(player, life) => player.GainLife(life * 2)
}

The last two examples look a little asymmetric, but that can be fixed with an apply method if desired.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.