The fuzzy concept in Scala

Source: Internet
Author: User
Tags abstract anonymous

Fuzzy concept list method and function covariant and contravariant lambda trait and Interface Curry Extractor object case-defined class-biased function implicit conversion and implicit parameter call-by namecall-by value

Fuzzy Concept list

First record, follow-up after the focus of the breach method and function covariant and inverse lambda what exactly is the difference between trait and interface currying (currying) Extractor object implicit conversion and implicit argument case definition class differs from ordinary class Bogwang parameter and pass-value parameter partial function square Methods and Functions

Reference article: Scala defines methods and functions differently: The method defined by Def, the function is defined by Val/var

def m (x:int) = 2*x
val f = (x:int) and x*2
No Parameter form: The method can have no argument list, but the function must have
Method
Def a =

//function
val b= =  23
Called differently: The method name represents a method call, and the function name only represents the function itself
scala> def m = m:int//You can execute m as a final expression on the command line, but actually perform a function call scala> m res7:int = scala& Gt def m1 (X:int) = x*2 M1: (x:int) Int//Can not execute M1 as a final expression because there are parameters scala> M1 <console>:13:error:missing argument lis
T for method M1 unapplied methods was only converted to functions when a function type is expected.
       You can make this conversion explicit by writing ' M1 _ ' or ' M1 (_) ' instead of ' M1 '. M1 ^//execution time brackets and Parameters scala> M1 (3) Res11:int = 6 scala> def f = () =>24 f: () = INT//no parameter in form, also must have argument list () , you can use F as the final expression, but the execution result is a structural description of the function scala> F res10: () = Int = $ $Lambda $1107/814300680@2e86807a//execution must be in parentheses scala> F
() Res12:int = scala> def f1 = (x:int) + 2 * x F1:int = Int//parameter function, or use function name (F1) as final expression scala> F1 Res13:int = Int = $ $Lambda $1108/1842571958@50a13c2f//execution, parentheses and parameters scala> F1 (2) Res15:int = 4 
The method is automatically converted to a function (ETA expansion): The place that bothers us is the place where we need to pass the function, we can also pass the method, so we will wonder how the method can be coerced into a function: add _ After the method, remember the middle there is a space oh.
Scala> def M4 (x:int) = 3*x
M4: (x:int) Int
scala> val f5 = M4 _
f5:int = Int = $ $Lambda $1138/11753 68310@4ffe3d42
Bogwang parameter: Method of non-parameter Covariance and Contravariance

Reference article: Understanding of type change, inversion and covariance
Scala Learning: Contravariance and covariance

+a: Co-change
-A: Inverter

Simply put, if person[+a], then if you define 2 person

Val person1 = new PERSON[A1]
val person2 = new PERSON[A2]

If A1 is a subtype of A2, then Person1 is also the subtype of person, which is the opposite of covariance.

The parameters of the function are contravariant, and the return of the function is covariant. Lambda

Reference article: What is the use of LAMBDA expressions. How to use it.
Deep understanding of Lambda
Java 8 lambda expression Adventure

A nameless function with the = = Trait and interface

First of all, the interface feature in Java, we can define the following information in the interface: keyword: interface attribute definition: But must be final static identity, that is, immutable, class properties, default does not need to add final static, automatically add Inherited identity: The other class inherits the interface by using the Implements keyword

Method definition: The interface can only have abstract methods, by default, the abstract keyword decoration

Inherit other interfaces: Inherit with extends, multiple separated by commas.

Next we will compare the next trait look: the keyword: trait. Attribute definition: Must be initialized, no matter with Val or var. Method definitions: Abstract methods and implemented methods are also available. Inherited: Extends, in multiple cases with the keyword link. Inherit multiple traits: extends, multiple with a link.

Overall, trait is more powerful than interface, you can define the method of implementation, or you can define variable properties Curry

To curry, a test (a:int,b:int) becomes test (a:int) (B:int) and Curry,test (2) returns a function. Extractor Object

The object containing the Unapply method, unapply In contrast to apply, apply assembles the passed in parameters into an object, unapply the object back into the passed argument. class defined by case

The following methods are automatically generated: Apply Unapply getter toString hashcode equals copy offset function

The anonymous function defined in map with case. implicit conversions and implicit arguments

Scala's implicit conversion

Implicit conversion is when a class lacks a method and you cannot modify the source code directly, you can use this function to extend the method.

Implicit conversions can understand a method that uses the implicit identity, which represents an implicit conversion to an object, similar to the type of the method (E:t) =>a, similar to the implicit conversion of T to A. After 2.10, Scala uses implicit to define classes to achieve implicit conversions.

Implicit parameters, in the method, a parameter defined by the implicit, especially in the Curry method, particularly useful, you can not give the implicit defined parameters, he will find the default definition of the value to fill, of course, you can also be explicitly assigned. If the parameter type is an anonymous function, then this implicit argument is also implicitly converted. call-by name/call-by Value

Scala basic Applications (10)-Values and Bogwang parameters

The value call (() =>unit), the value is evaluated first, then called, and the call must be added (). The Bogwang call (=>unit) is evaluated only when the first call is made, and the call only needs to be done with the parameter name.

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.