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: enter image description here , 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

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -