Deep understanding of the type system in Scala

Source: Internet
Author: User

Core content:
1. Two ways of defining types in Scala
2. Path dependencies in Scala
3. The type of self in Scala and the qualification of its own type
4. Dependency Injection in Scala
5. The use of chain style call--type mechanism in Scala
6. Composite data types in Scala
7. Abstract types in Scala
8. Type parameters in Scala
9. Type constraints in Scala
10. Type change, inversion and covariance in Scala
11. Structure types in Scala

1. Two ways of defining types in Scala

There are two ways to define a type in Scala:
① defining a class, trait, or object
② defining types directly with the Type keyword
In Scala, when labeling types, you can refer to their type directly with the name of the class and trait, to reference the type of the object, and to reference its type with the object's kind member.

2. Path dependencies in Scala

① in Scala, objects have different paths depending on their type, and the paths depend on each other, resulting in different types.
The ② path is not a type, but is similar to a location.

3. The type of self in Scala and the qualification of its own type

① Each class itself has a this pointer that can refer to its own instance through the this pointer, and self is not a keyword in its own type, it is the alias of this and is more readable.
② in Scala, you can constrain the type of this in the class, the trait, and the object in a way that restricts our type and behavior.


1, limit the creation of S2 objects, must be mixed with S1 this trait
2, S2 of the sub-class must also be mixed with S1 this trait

4. Dependency Injection in Scala

The dependency injection in ①scala is done in a way that is limited by its own type, and can be used with properties and methods in the carrying class after it is represented by its own type qualification.
Dependency Injection in ②scala can be seen as a third way to blend in with the traits in Scala.
③ through the dependency injection in Scala, you can make existing classes use the properties and methods in the Carry class, and the implicit conversion mechanism can do the same thing.

Example 1:

Example 2:

From the above program you can see the powerful implicit conversion function.

5. The use of chain style call--type mechanism in Scala

① in Scala, we can do chain-style programming because we use the type mechanism, the so-called singleton type, to concatenate some method calls together.
② in Scala, any class object has a type attribute, which may return a class or an object or a null value.
③this.type is a mechanism provided by Scala that points to the type of the current object.

6. Composite data types in Scala

A composite data type in ①scala is a concrete type that is constructed by combining other types, a new type that is called a composite data type.
Ii
A type is a composite data type, both B and C and D, which requires that a has both
B, but also has C, but also has the function of D. The syntax interpretation in Java differs from this.

7. Abstract types in Scala

① uses the Type keyword in Scala to define an abstract type, which is a type that does not indicate a specific type.
② in Scala, because sometimes the type parameter syntax is overly complex, so you can use abstract types whenever possible.

Example Program 1:

Example Program 2:

8. Type parameters in Scala

① in Scala, the type parameter is defined by the square brackets [] before the normal parameter, and then the normal parameter can be used as the type parameter as the type parameter, the simple method of the type argument definition:

② type parameters are much like method parameters, except that they are parameterized at compile time.

9. Type constraints in Scala

① in Scala, the restriction (constraint) of a type variable is divided into two types: the upper bound of the type variable and the lower bound of the type variable. Through the qualification of type variables, we can easily express the characteristics and methods that the type variable has some needs.
The upper bounds of the ② type variable are defined as follows:

The lower bound of a type variable is defined as follows:

③ in Scala, the maximum upper bound for all types is any, and the maximum lower bound is nothing.
Because all types in Scala inherit from any, and all types are inherited by nothing.

10. Type change, inversion and covariance in Scala

① type variable refers to the ability to change or change the type parameters of higher-order types like t[a], with three forms: invariant (invariance), covariant (covariance), inversion (contravariance). Covariance is the ability to replace a type parameter with its parent class. That is the change of compromise. The inverse is the ability to replace the type parameter with its subclass, and the inverse of the day.
② If a type parameter supports covariant or contravariant, the type parameter is called variance (variable), otherwise it is called immutable.
③ a mutable generic type does not inherit from the quilt class, and if the parent class is declared mutable, the subclass needs to be declared as a mutable type again if it wants to keep the mutable type.
④ for a function, the parameter type of the function is contravariant or invariant, and the return value type of the function is covariant or invariant.
⑤ in general, when encountering covariant and contravariant faults in a class method, the usual solution is to introduce a new type parameter, that is, the newly introduced type parameter in the method signature.

11. Structure types in Scala

A ① struct type is a set of descriptions of the specifications of an abstract method or field or type that are required by the parameters we pass in: that is, the arguments or objects that are passed in must have the appropriate abstract method or field or type.
② in Scala's structure type, we don't care about the type of objects passed in, we have only one requirement for the arguments or objects that come in: that is, the arguments or objects that are passed in have some kind of behavior.

If you have any questions, please feel free to comment!

Deep understanding of the type system in Scala

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.