Learn Scala together with Lian le-advanced type, scala-type

Source: Internet
Author: User

Learn Scala together with Lian le-advanced type, scala-type

Welcome to join Scala discussion QQ Group 212859367, and join!

Singleton type
Example:

class Document{    def setTitile(title: String) = {...; this}    def setAuthor(author: String) = {...; this}    ...}val atticle.setTitle("whatever floats your boat").setAuthor("lianle")

If the above classes have child classes, problems will occur:

Class Book extends Document {def addChapter (chapter: String) = {...; this }...} // call error: val book = new Bookbook. setTitle ("Scala for the impatient "). addChapter ("chapter1 ")

Cause analysis:
Because setTitle returns this; Scala infers the return type to Document, but the Document does not have the addChapter method.

Solution:

Def setTitle (title: String): this. type = {...; this} // Add the singleton type

Type projection
Example:

import scala.collection.mutable.ArrayBufferclass Network {    class Member(val name: String) {        val contacts = new ArrayBuffer[Member]    }    private val members = new ArrayBuffer[Member]    def join(name: String) = {        val m = new Member(name)        members += m        m    }}

Define two networks:

val chatter = new Networkval myFace = new Network

The following operation will report an error:

Val fred = chatter. join ("Fred") // The type is chatter. memberval barney = myFace. join ("Barney") // The type is myFace. memberfred. contacts + = barney // Error

Solution:

Class Network {class Member (val name: String) {val contacts = new ArrayBuffer [Network # Member] // The above indicates "any Network Member "}}

Type alias

For complex types, you can use the type keyword to create an alias.

Class Book {import scala. collection. mutable. _ type Index = HashMap [String, (Int, Int)] //, you can use Index instead of HashMap .}

Aliases must be nested in classes or objects.

Structure Type

The so-called "structure type" refers to a set of specifications for abstract methods, fields, and types. These abstract methods, fields, and types are required for the type that meets this specification.

def appedLines(target: {def append(str: String): Any}, lines: Iterable[String]) {    for(l <- lines){ target.append(1);target.append("\n")}}

You can call the appendLines Method for any instance with the append method.

Composite Type
Definition form:

T1 with T2 with T3 ...

Example:

val image = new ArrayBuffer[java.awt.Shape with java.io.Serializable]
Shape with Serializable {def contains (p: Point): Boolean} // This type of instance must be both a child type of Shape and a child type of Serializable.

Center type

The center type is a type with two type parameters.

String Map Int // String and Int types. // Similar to A * B in Mathematics

Existing type

Existing types are added to Scala to be compatible with Java type wildcards.
Definition form: Follow forSome {…} after the type expression {...}, The curly braces contain the declaration of type and val.

Array [T] forSome {type T <: JComponent} is equivalent to: Array [_ <: JComponent]
Array [_] is equivalent to: Array [T] forSome {type T}
Map [_, _] is equivalent to: Map [T, U] forSome {type T; type U}

ForSome allows us to use more complex relationships, not just those that can be expressed by type wildcards.
Map[T, U] forSome {type T; type U <: T}

Scala Type System

Type Syntax example
Class or trait Class C ..., Trait C...
Tuples (T1, T2 ,... Tn)
Function Type (T1, T2... Tn) => T
Annotation type T @
Parameterized type A [T1, T2... Tn]
Singleton type Value. type
Type projection O # I
Composite Type T1 with T2... With Tn {declaration}
Center type T1 A T2
Existing type T forSome {type and val declaration}

Type

Form:

This: TYPE =>

Example:

Trait Logged {def log (msg: String)} trait LoggedException extends Logged {this: Exception => def log () {log (getMessage ()} // you can call getMessage, because this is an Exception}

Dependency Injection

Trait LoggerComponent {trait Logger {...} val logger: Logger class FileLogger (file: String) extends Logger {...}} trait Auth {this: LoggerComponent => // Let's access the logger trait Auth {...} val auth: Auth class MockAuth (file: String) extends Auth {...}...}

Assembly parts:

object AppComponents extends LoggerComponent with AuthComponet {    val logger = new FileLogger(text.log)    val auth = new MockAuth("users.txt")}

Abstract type

A class or trait can define an abstract type that is embodied in a subclass.

trait Reader {    type Contents    def read(fileName: String): Contents}

Implementation:

class StringReader extends Reader {    type Contents = String    def read(fileName: String)  = Source.fromFile(fileName, "UTF-8").mkString}class ImageReader extends Reader {    type Contents = BufferedImage    def read(fileName: String) = ImageIO.read(new File(fileName))}

Another example:

trait Reader[C]{    def read(file: String) : C}class StringReader extends Reader[String] {    def read(fileName: String) = Source.fromFile(fileName, "UTF-8").mkString}class ImageReader extends Reader[BufferdImage] {    def read(fileName: String) = ImageIO.read(new File(fileName))}

Experience:

1. If the type is provided when the class is instantiated, the type parameter is used.
2. If the type is given in the subclass, the abstract type is applicable.

Higher type

The generic type List depends on type T and generates a specific type.
Generic types such as List are sometimes called type constructors.
In Scala, you can define types that depend on other types.

Welcome to join Scala discussion QQ Group 212859367, and join!

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.