English 中文(简体)
寻找如何在Scala中进行模式匹配时使用“@ _ *”的示例
原标题:Looking for examples of how to use "@_*" when doing pattern matching in Scala

我已经搜索了一会儿,但找不到任何例子来演示在匹配案例类时使用@_*的用法。

以下是我提到的那种申请的例子。

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?
I'm sorry, you forgot to provide the text that needs to be translated. Please provide the text and I'll be happy to assist you.

I'm sorry, you forgot to provide the text that needs to be translated. Please provide the text and I'll be happy to assist you.

如果有人能展示给我一些例子或者指引我到一个网站,那就太好了。

谢谢 (xiè xiè)

最佳回答

请记得像这样的事情

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

radius绑定到值3.0,并将t2绑定到正在匹配的Type2实例。

使用您的例子:

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(_))

}

在这里,group 绑定到定义 MixedTypesSomeObject 序列。您可以将其视为与 MixedTypes 构造函数参数相同的序列。

问题回答

Wampler/Payne的节目编排表an example

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

关于unapplySeq的Daily Scala博客帖子:unapplySeq





相关问题
Declaring function objects for comparison?

I have seen other people questions but found none that applied to what I m trying to achieve here. I m trying to sort Entities via my EntityManager class using std::sort and a std::vector<Entity *&...

Equality Test for Derived Classes in C++ [duplicate]

Possible Duplicate: What’s the right way to overload operator== for a class hierarchy? In C++, how can derived classes override the base class equality test in a meaningful way? For ...

Is there an Non-Short circuited logical "and" in C++?

tl;dr: Is there a non-short circuit logical AND in C++ (similar to &&)? I ve got 2 functions that I want to call, and use the return values to figure out the return value of a 3rd composite ...

Javascript String Assignment Operators

How come I can use += on a string, but I cannot use -= on it? For example... var test = "Test"; var arr = "&#8660;" test += arr; alert(test); // Shows "Test&#8660;" test -= arr; alert(...

PHP Comparison Operators and Data Types

I m currently working through O Reilly s "Programming PHP" and have come across this table titled "Type of comparison performed by the comparison operators": First Operand | Second ...

热门标签