C # Programmer's Scala path tenth (pattern matching)

Source: Internet
Author: User

1. Sample class

Add the keyword case to the class before the class is the sample class, and the sample class is instantiated without the new keyword as follows:

 Case class  extends  App {  = A  // using a sample class after  new  A}

The second advantage of the sample class is that he makes your class parameter primers get Val, as follows:

 Case class  extends  App {  = A ("name")  println (a.name)}

2. Pattern matching

Pattern matching is a powerful feature, with the following code:

Abstract classBase Case classA (Name:string,age:int)extendsBase Case classB (age:int,name:string)extendsBaseobject HelloextendsApp {def basematch (base:base): String=Base match{ CaseA (value,21) =value CaseB (Value, "name2") = =value.tostring Case_=>NULL} val a= A ("name", 21) Val B= B ("name2")) println (Basematch (a)) println (Basematch (b))}

The key is that case A (value,21) and Case B (value, "name2") pattern matching can match the values in the object,

It is similar to switch, but changes more than switch, he can directly match the values in the object, or directly match the object.

3. Variable mode and constant mode

Look at the following code:

extends App {  =    expr match{    case 0=> ' zero ' case     value = ' Value: ' + Value  }  println (Matchany (0))  println (Matchany ("123456"))  println (Matchany(123456 ))}

Value uses the variable pattern, and 0 uses the constant pattern.

The variable pattern is to match the variable itself as a wildcard character.

4. Sequence mode

The following code:

extends App {  =    expr Match {      case List (0, 1, _, value) + =      valuecase  _ = Nil    }  println (Matchany (0))  println (Matchany (List (0, 1, 4, 5) ))  println (Matchany (List (0, 1, 3, 9))}

5. Tuple mode

The following code:

extends App {  =    expr Match {      case (A, b, c) = = println ("Matched:" + A + B +  c) 
     Case _ = Nil    }  tupleany (("A", 1, 3.14)}

6. Type mode

The following code:

extends App {  =    value Match {      case x:string = x.length      case  Map:map[_, _] = Map.size      Case _ = Nil    }  println (Typematch (" String "))  println (Typematch (Map (1," A ", 2," B "))}

7. Mode Guard

The following code, after matching an expression, is preceded by an if statement:

Object Hello extends App {  =    value Match {      case List (1,2 ,3  if e = = y = = e      casefalse    }  println ( Typematch (List (1,2,3,5,5)))}

For more information, please refer to Scala programming

C # Programmer's Scala path tenth (pattern matching)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.