Look for examples of using "@_ *" when matching patterns in Scala

I searched a bit, but cannot find examples demonstrating the use of @_ * classes when matching classes with a pattern.

Below is an example of the application that I have in mind.

def findPerimeter(o: SomeObject): Perimeter = o match { case Type1(length, width) => new Perimeter(0, 0, length, width) case Type2(radius) => new Perimeter(0, 0, 2*radius, 2*radius) ... case MixedTypes(group @_*) => { \\How could @_* be used to check subpatterns of group? } 

}

If someone can show me some examples or point me to a web page that has some examples that would be great.

thanks

+7
operators scala pattern-matching
source share
2 answers

Remember that something like

 Type2(3.0) match { case t2 @ Type2(radius) => //... } 

associates radius with a value of 3.0 and associates t2 with an instance of type 2 that maps to.

Using your example:

 def findPerimeter(o: SomeObject): Perimeter = o match { case Type1(length, width) => new Perimeter(0, 0, length, width) case Type2(radius) => new Perimeter(0, 0, 2*radius, 2*radius) // ... // assume that Perimeter defines a + operator case MixedTypes(group @ _*) => group.reduceLeft(findPerimeter(_) + findPerimeter(_)) } 

Here group bound to the SomeObject sequence, which defines MixedTypes . You can think of it as a sequence of any-args-for-MixedTypes-is constructors.

+5
source share

Programming Scala Wampler / Payne has an example .

Also another SO question: Pattern matching String as Seq [Char]

And the daily Scala blog post on unapplySeq .

+3
source share

All Articles