Scala: What is :: in { case x :: y :: _ => y}? Method or case class? -
object test1 extends app { val list: list[int] => int = { case x :: y :: _ => y //what ::? method or case class? } println(list(list(1, 2, 3))) //result 2. } i set "syntax coloring" in scala ide, foreground color of method set red. code snapshot:
, can't open declaration of black ::, don't know is.
if black :: method, should called way:
... {case _.::(y).::(x) => y} //compile failed! so, black ::? method or case class?
thanks lot!
i think it's method described here. history's sake, in case page goes away, here's blurb:
about pattern matching on lists
if review possible forms of patterns explained in chapter 15, might find neither list(...) nor :: looks fits 1 of kinds of patterns defined there. in fact, list(...) instance of library-defined extractor pattern. such patterns treated in chapter 24. "cons" pattern x :: xs special case of infix operation pattern. know that, when seen expression, infix operation equivalent method call. patterns, rules different: when seen pattern, infix operation such p op q equivalent op(p, q). is, infix operator op treated constructor pattern. in particular, cons pattern such x :: xs treated ::(x, xs). hints there should class named :: corresponds pattern constructor. indeed there such class. named scala.:: , class builds non-empty lists. :: exists twice in scala, once name of class in package scala, , again method in class list. effect of method :: produce instance of class scala.::. you'll find out more details how list class implemented in chapter 22.
so it's scala.::(a,b)
Comments
Post a Comment