Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - designation of a matched item in pattern matching

I have the following code:

class Animal(hair: Option[Hair])

class Cat(var hair: Option[Hair]) extends Animal(hair)
class Dog(var hair: Option[Hair]) extends Animal(hair)
class Sheep(var hair: Option[Hair]) extends Animal(hair)

//then somewhere else:

def what(animal: Animal) {

  animal match {
    case Cat(hair) => println("processing cat, hair=" + hair)
    case Dog(hair) => println("processing dog, hair=" + hair)
    case Sheep(hair) => {
      println("processing sheep, cutting hair...")
      hair = None
    }
  }
}

The questions are:

1) When pattern matching succeeds with a Sheep, how can I access it's hair and change it? It complained about reassignment to val, and I have then placed var in the constructor but still...

2) One more way I can think of is to assign the entire matched value to a variable, is there any way to bind a value matched by some case class constructor pattern to a variable?

(I know that I could probably pattern match on something like s: Sheep and then call s.changeHairTo(None) but that is the least preferrable way).

like image 767
noncom Avatar asked Mar 10 '12 08:03

noncom


1 Answers

You can use @ to bind the whole pattern to variable in your version

class Animal(hair: Option[Hair])
case class Cat(var hair: Option[Hair]) extends Animal(hair)
case class Dog(var hair: Option[Hair]) extends Animal(hair)
case class Sheep(var hair: Option[Hair]) extends Animal(hair)

def what(animal: Animal) {
  animal match {
    case Cat(hair) => println("processing cat, hair=" + hair)
    case Dog(hair) => println("processing dog, hair=" + hair)
    case s @ Sheep(hair) => {
      println("processing sheep, cutting hair...")
      //cut(hair)
      s.hair = None
    }
  }
}

But you don't have to use var. Here is more functional version of your snippet. what here just returns Sheep with None Hair after cutting.

trait Animal
case class Cat(hair: Option[Hair]) extends Animal
case class Dog(hair: Option[Hair]) extends Animal
case class Sheep(hair: Option[Hair]) extends Animal

def what(animal: Animal): Animal =
  animal match {
    case Cat(hair) => {
      println("processing cat, hair=" + hair)
      animal
    }
    case Dog(hair) => {
      println("processing dog, hair=" + hair)
      animal
    }
    case Sheep(hair) => {
      println("processing sheep, cutting hair...")
      //cut(hair)
      Sheep(None)
    }
  }
}
like image 125
4e6 Avatar answered Nov 16 '22 01:11

4e6