It's really love and hate to have so many grammatical sugars and new concepts for Scala. Love is that Scala introduces a lambda feature that Java has never had, which is very love for working with collection data using higher-order function abstractions (Spark's simple RDD processing benefits from this). The hate is that Scala does so much new concepts and grammatical sugars.
Here are some of the syntactic sugars and new concepts:
Singleton objects (Singleton object)
Scala does not have the static keyword and creates an object keyword to create a new singleton. The members in the Singleton object are static. So to write Util class is generally to use this thing.
Object Xxutil {def process (xx:string): String = {XX}}
Ii. associated objects and associated classes (Companion Object & Companion Class), independent objects (standalone object)
The two concepts are mutual. Assume that there is an object A and class A two with the same name. At this point it can be said that object A is the "associated object" of Class A, and Class A is the "companion class" of object A. When an object B does not have a class B with the same name, object B is called "Make an Independent object".
The attendant privilege is that they can access private members to each other.
The difference between class and object:
1. Singleton objects cannot be initialized with new.
2, the Singleton object can not take parameters.
3. The singleton object is initialized when the first call is initiated.
Third, slightly pit function call syntax sugar
1, the magic point number omitted.
While some times bring some convenience, the combination of lambda features and other things, the code can simply light up your kryptonite dog eyes.
No parameter "Hello" touppercase "Hello". toUpperCase "Hello". toUpperCase ()//a parameter "Hello". IndexOf "H" "Hello" indexOf "H"//Multiple parameters "Hello World" substring (0, 3)//All Together "Hello World" substring (0, 3) touppercase () indexOf "H"//Mates on anonymous function array (all-in-a-line) map ((i:int) = i+1) 1 to 3 map ((i:int) = i+1)
2. Magical for
This for is normal, right? for (I <-1 to 4) println (i)//this is it! for (I <-1 to 4 if I > 1) println (i)//this Is it!!! for (I <-1 to 4 if I > 1; if i < 4; if I% 2 ==0) println (i)
3, Magical curly braces {} instead of parentheses () syntax
It is said that if a function call passes only one argument, the curly braces can replace the parentheses (the last bracket can also be replaced), and Scala Baba will not spank your hips.
println ("Hello") println{"Hello"}def xx (I:int) (j:int) = I+j xx (1) {2}//result:3 (XX (1) _) {3}//curry call (XX (1) _) (3)// Curry call, do not believe you do not know//read the above is not ignorant? Then one more def xx (I:int) (J: (Int) =>int) = J (i) XX (1) {i=> i+10}//have a love side//Suppose I define an hbase scan method def scan (tablename:string, Cf:string, Startrow:string, stoprow:string) (PROCESSFN: (Result) = = Unit) = {//...} So I can call scan so naturally (T1, CF, StartRow, stoprow) {r =//todo process result}
4. Magical ByName functions and calls
In order for the method you define to look as "harmonious" as the Scala built-in syntax, a byname parameter and a byname function are developed.
Examples of copying Scala programming Def myassert (pred: () =>boolean) = if (!pred ()) throw new Assertionerrormyassert (() = 5<3)//() = > 5<3 anonymous function Does it look like it's uncomfortable? Because the () = = 5<3 Call does not look natural. So remove this part of the (). def myAssert2 (pred: = = Boolean) = if (!pred) throw new AssertionErrormyAssert2 (5<3)//This looks much more cool.
Iv. types of upper and lower bounds
Class Foo[t <% a]{...}//weak upper bound <%. Weak relationship: T can be implicitly converted to Ordered[t]class foo[t <: a]{...}//upper bound <:. T must be a subclass of a, and a is the upper bound of type T. Class foo[t;: a]{...}//Nether; T must be the parent of a, and a is the lower bound of type T.
V. Covariance and Contravariance
+t: Covariant: Class Queue[+t] {}. If c<:a, then queue[c] <: Queue[a]
-T: contravariant: Class Queue[-t] {}. If c<:a, then queue[c];: Queue[a]
VI. Implicit conversion
Implicit conversions, which correspond to explicit conversions. For example, there is "1234". ToInt, this is the explicit conversion, not the direct call ToInt method is the implicit conversion.
Implicit def str2int (s:string): Int = Integer.parseint (s)//implicit str to intdef Add (a:int, b:int) = A+badd ("1", 2)//1 is implicitly converted to 1, Add up again.
vii. Case Category (Reprint Note: template class, is designed for pattern matching)
The case class is a class, but it can be used to do pattern matching. So I got this thing.
The difference between the case class and the General class:
1, there will be the same as the class name of the factory method. You do not need to use new to create this class object.
Case class Var (name:string) Val v1 = var ("Peter")//and the same factory method as the class name
2. The parameter list of case class is Val by default.
3, the natural implementation of tostring,hashcode and Equals is added automatically.
The biggest use of case classes is to do pattern matching.
Case class Var (name:string) Val v1 = var ("Peter") V1 matches {case Var (name) + = Name Case _ =>}//match out name to Peter
Eight, Mode guard
Mode Guard (Pattern Guard) follows the pattern and begins with If. Only the mode guard returns true to be successful.
Ten match {case N:int if 1< n = println (n)//if to + = Before this paragraph is a pattern guard case _ =>}
About the new concept of the grammar sugar in Scala