Help us understand the problem. What is going on with this article?

[Scala]パターンマッチにおけるアットマーク(@)

More than 5 years have passed since last update.

参照

http://stackoverflow.com/questions/4513380/scala-match-help

問題

こんなコードがある

def flatMapSublists[A,B](ls: List[A])(f: (List[A]) => List[B]): List[B] = 
  ls match {
    case Nil => Nil
    case sublist@(_ :: tail) => f(sublist) ::: flatMapSublists(tail)(f)
  }

ん?sublist@(_ :: tail)???

意味

パターンマッチを行う際にfoo@barで、barにマッチしたものをfooにバインドするということらしい
つまり、上の例ではls_ :: tailにマッチするようなものだった場合に、lssublistにバインドされる

他の例

参照URLで出されている例そのままだが、リストに3つの要素があるかどうか確認し、
そうであった場合に、標準出力しながら、リストの2番目(indexは1)の要素を取得したいとき

val something = List(1, 2, 3)
something match {
  case ls@List(_,elem,_) => println("matching:" + ls); elem    
  case _ => println("not found")
}   
// res: AnyVal = 2

// @を使わずに同様のことを行う
something match {
  case List(a,elem,b) => println("matching:" + List(a,elem,b)); elem    
  case _ => println("not found")
}   
// res: AnyVal = 2

// これでも良い
something match {
  case List(a,elem,b) => println("matching:" + something); elem    
  case _ => println("not found")
}  

ls@なんて付けずにsomethingをそのまま使えばいいんじゃね?って思った
よくわからんのでもう少し

val list = List(1, 2, 3)

list match {
  case ls@(h :: tail) => println(tail); list
  case Nil => Nil
}
// List(2, 3)
// res: List[Int] = List(1, 2, 3)

list match {
  case ls@(h :: tail) => println(tail); ls
  case Nil => Nil
}
// List(2, 3)
// res: List[Int] with Serializable = List(1, 2, 3)

なぜかSerializableになった
この@には名前がないのかな

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした