Scala pattern matching and type system

Source: Internet
Author: User
Tags switch case

1. Pattern matching is much more powerful than switch case in Java, in addition to matching values, types, collections, and so on, the most common case class matches, and Master.scala has a large number of pattern matches.

Case "_" means an experience that does not satisfy all of the above scenarios, for example:

def bigdata (data:string) {

Data match{

Case "Spack" =>PRINTLN ("WOW!!!")

Case "Hadoop" =>println ("OK")

Case _=>println ("sorry")

}

}

Bigdata ("spack")//WOW!!!

You can add conditional judgment to a case.

def bigdata (data:string)

{

Data match{

Case "Spark" =>println ("wow!!")

Case "Hadoop" = println ("OK")

Case _ if data = = "Flink" =>println ("Flink")

Case _=>println ("other")

}

}

Bigdata ("Flink")//flink

To match a type

Def exception (e:exception) {

E match{

Casefileexception:filenotfoundexception =>println ("File not fount:" +fileexception)

Case _:exception=>println ("Exception", E)}

}

Exception (new filenotfoundexception ("OOP!!!")) > File notfount:java.io.FileNotFoundException:oop!!!

To match a collection

def data (array:array[string])

{Arraymatch{

Case Array ("Scala") =>println ("Scala")

Case Array (Spark,hadoop,flink) =>println (Spark + ":" +hadoop + ":" +flink + ":")

Case Array ("Spark", _*) =>println ("Spark ...")

Case _=>println ("Unkown")

}

}//> Data: (array:array[string]) Unit

Data (Array ("Spark"))//> Spark ...

Data (Array ("Scala"))//> Scala

Data (Array ("Scala", "Spark", "Kafaka"))//> Scala:Spark:kafaka:

to match a class

Scala> Case Class Person (name:string)

Defined class person

Case Class Person (name:string)

Person ("Spark")//Res0:worksheetest. person = person (Spark)

1:case class has only one get in relation to Bean,val in Java

2: Instance automatically calls the associated object

Class Person

Case Classworker (name:string,salary:double) extends person

Case Classstudent (name:string,score:double) extends person

def sayhi (Person:person)

{

personmatch{

Case Student (Name,score) =>println ("I am Student:" +name + "," +score ")

Case Worker (name,salary) =>println ("I AM Worker:" +name + "," +salary ")

Case _ =>println ("Unknown")

}

}//> Sayhi: (person:worksheetest. person) Unit

Sayhi (worker("worker", 6.5))//> I am worker:worker,6.5

Sayhi (Student ("Student", 6.5))//> I am student:student,6.5

Deploymessages Source code:

Caseclassexecutorstatechanged (

AppId:String,

Execid:int,

State:executorstate,

message:option[String],

Exitstatus:option[int])

Extends Deploymessage

Many objects are generated when case class is used

Case object itself is an instance, globally unique

Scala's type parameters (Heavyweight stuff) The best difficulty, too useful, in all spark source code is everywhere

Example: Rdd[t:classtag]

Generics, the parameter itself is a generic type, Scala,

generic classes and generic functions

Class Person[t] (valcontent:t)

{

def getcontent (id:t) = id+ "_" + content

}

Val p = newperson[string] ("Spark")//> p:worksheetest. Person[string] = [email protected]

P.getcontent ("Scala")//> res0:string = Scala _ Spark

Generics are preceded by + and-

* scala> def Mkarray[t:classtag] (elems:t*) =array[t] (elems: _*)
* Mkarray: [T] (elems:t*) (implicit evidence$1:scala.reflect.classtag[t]) array[t]
*
* Scala> Mkarray (42, 13)
* Res0:array[int] = Array (42, 13)
*
* scala> Mkarray ("Japan", "Brazil", "Germany")
* Res1:array[string] = Array (Japan, Brazil, Germany)
* }}}

Covariant: If S is a subtype of T and List[s] is also a subtype of list[t], then it becomes covariant class person[+t]//coercion is defined as covariant type

C[+T]: If A is a subclass of B, then C[a] is a subclass of C[b]. Small inverter range
C[-T]: If A is a subclass of B, then C[b] is a subclass of C[a]. Large covariant Range
C[T]: Regardless of the relationship between A and B, C[a] and c[b] have no dependencies.

Note: Read the source code for the spark source RDD, Hadooprdd, Sparkcontext, Master, and worker, and analyze the contents of all pattern matching and type parameters used inside.

Summarize:

T <% Writable:classtag
T can be converted to writable type by stealth
Classtag injection of implicit values in context

For manifest Context Bounds
[T:manifest] evolved to Classtag, T:classtag the runtime passes complete type context information

Seq[dependency[_]] [equivalent to seq[dependency[t]]

There is another important note:

{{{
* scala> def Mkarray[t:classtag] (elems:t*) = Array[t] (elems: _*)
* Mkarray: [T] (elems:t*) (implicit evidence$1:scala.reflect.classtag[t]) array[t]
*
* Scala> Mkarray (42, 13)
* Res0:array[int] = Array (42, 13)
*
* scala> Mkarray ("Japan", "Brazil", "Germany")
* Res1:array[string] = Array (Japan, Brazil, Germany)
* }}}
*

The implicit conversion mechanism of Classtag is shown.

Scala pattern matching and type system

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.