Understanding the type system in Scala

Label: Scalastructure type
6016 reading comment(0) Collection Report
Classification:

Core content:
1, Scala defines the type of the two ways
2, path dependence in Scala
3, Scala in their own types and their own types of restrictions
4, dependency injection in Scala
5, Scala chain style call - the use of -Type mechanism
6, Scala composite data type
7. Abstract types in Scala
8, type parameters in Scala
9, type constraints in Scala
In 10, Scala, inverter, covariant variable
11, the type of structure in Scala


1, Scala defines the type of the two ways

In Scala, you can define the type in two ways:
Defining classes, attributes, or objects
Define the type directly with the type keyword
In Scala, you can use the name of the class and the name of the character to refer to the type of the type directly. To quote the type of the object, you need to use the type member of the object to refer to the type.


Here to write picture description


2, path dependence in Scala

In Scala, the path dependence of the object is different, its type is not the same, the path dependence is different, resulting in different types.
The path is not a type, but a similar position.


Here to write picture description


3, Scala in their own types and their own types of restrictions

Each class itself has a this pointer, through the this pointer can refer to its own instance; in its own type, self is not a keyword, is the this alias, with a stronger readability.
In the Scala, can be in the class, the characteristics of the Object in their own type of this constraints, through their own types of restrictions, can be very good to limit our type and behavior.
3
Here to write picture description
1, limit the creation of S2 objects, must be mixed with the characteristics of S1
2, S2 subclasses must also be mixed with S1 this trait


Here to write picture description


4, dependency injection in Scala

The dependency injection in Scala is accomplished by means of its own type constraints, which can be used to carry the attributes and methods in the class
Scala dependency injection can be seen as a mixture of Scala among the third ways
Through the dependency injection in Scala, we can make use of the attributes and methods in the class to carry out the same function.


Example 1:
Here to write picture description
Example 2:
Here to write picture description
From the above procedures can be seen that the powerful conversion function.


5, Scala chain style call - the use of -Type mechanism

In the Scala, we can do the chain style programming, because we use the type mechanism, which is called a singleton type of way, some of the method calls in series
In Scala, any class object has a type property, which may be either a class or an object, or an empty value, in type.
This.type is a mechanism provided by Scala that points to the type of the current object.


Here to write picture description


6, Scala composite data type

The composite data type in Scala is the specific type constructed by combining other types, and the new type is called the compound data type
2Here to write picture description
The A type is a composite data type, both B and C is a sub type of D, that is, the requirements of both A
B, but also has C, but also has the function of D. The syntax of Java is different from this.


Here to write picture description


7. Abstract types in Scala

In Scala, the type keyword is used to define an abstract type, and an abstract type is a type that does not specify a specific type
In Scala, because sometimes the type parameter grammar is too complex, we can use the abstract type as far as possible


Sample program 1:
Here to write picture description
Sample program 2:
Here to write picture description


8, type parameters in Scala

In Scala, the type parameter is in general use before the parameters defined in brackets [], then the common parameters can be used as a parameter type parameter type, simple type parameter definition method:
Here to write picture description
Type parameters are very much like the method parameters, but they are parameterized at compile time


Here to write picture description


9, type constraints in Scala

In Scala, the constraints of the type variables are divided into two types: the upper bound of the type variable and the lower bound of the type variable. By defining the type variables, we can easily express the types of variables with certain characteristics and methods
Definition of upper bounds of type variables:
Here to write picture description
Lower bounds for type variables:
Here to write picture description
In Scala, the maximum upper bound of all types is Any, and the lower bound is Nothing.
Because all of the types in Scala are inherited from Any, and all types are inherited by Nothing


Here to write picture description


10, Scala type variable, contravariant and covariant

The type variable refers to the ability parameters of high-order types like T[A] can change or change the type variable has three forms: constant (Invariance) and the covariance (Covariance), inverter (Contravariance). The covariant refers to the type parameters to replace the ability of the superclass, i.e. compromise inverter change. Refers to the type parameters to replace the ability of its subclasses, and change of guards.
If a type parameter of a covariant or contravariant support, is called the type parameter is Variance (variable), otherwise known as immutable.
A mutable generic type does not inherit from a class, and if the parent class is declared to be mutable, the subclass needs to be declared as a mutable type again if you want to keep the variable type
For the function, the function parameter type is inverse or invariant, and the return type of the function is covariant or constant.
In general, when the fault with covariant and contravariant methods in the time, the usual solution is to introduce a new type of parameters, with the introduction of the new type parameters in the method signature.


Here to write picture description


11, the type of structure in Scala

The structure type is about a set of abstractions or fields or types of specifications, these abstract methods or fields or types of parameters we passed must have: parameter or object passed must have abstract methods or fields or types of corresponding.
In the Scala structure type, we do not care about the type of the object to be passed, we have only one requirement for the parameters or objects to be passed.


Here to write picture description


If you have any questions, please comment!

top
Three
step on
Zero

Reference knowledge base

Guess you are looking for
View comments
* the above user comments represent their personal views and do not represent the views or positions of the CSDN website
    personal data
    • Visit:264030 times
    • Integral:Five thousand three hundred and forty-five
    • Grade:
    • Ranking:4071st place
    • Original:268
    • Reprint:34
    • Translation:0
    • Comment:42
    Blog column
    Article classification
    Latest comments