Scala pattern matching 2 How is Scala implemented?

Source: Internet
Author: User

In this Martin and two other pattern matching experts ' papers, there are several ways to implement pattern matching, and Scala chooses which way to implement it.
Http://lampwww.epfl.ch/~emir/written/MatchingObjectsWithPatterns-TR.pdf
I quoted some of the descriptions inside.

In object-oriented programs, data is organized into class-level classes
The problem with object-oriented language in pattern matching is how to detect this level from the outside.

There are 6 ways to implement pattern matching:
1) Object-oriented decomposition (decomposition)
2) accessor mode (visitor)
3) Type test/type styling (Type-test/type-cast)
4) Typecase
5) Sample class (case Class)
6) decimation Device (extractor)

In this paper, we compare various implementations from 3 dimensions to 9 standards:
Simplicity (frame support, shallow matching, deep matching), maintainability (characterization Independent, extensibility), performance (base performance, breadth, and depth ductility)


The details of the comparison are mentioned in this paper and are not unfolded.
Finally, Scala chooses to use the sample class (case Class) and the extractor (extractor) to achieve pattern matching.

Let's take a general look at the case class and extractor.

1) Sample class (case Class)

In essence, case class is a syntactic sugar that adds getter access to your class construction parameters, as well as ToString, hashcode, Equals, and so on.
The most important thing is to help you implement a companion object, which defines the Apply method and the Unapply method.
The Apply method is used to reduce the new keyword when constructing an object, while the Unapply method serves for pattern matching.
These two methods can be considered as two opposite behaviors, apply is construction (Factory mode), Unapply is decomposition (deconstruction mode).

Case class exposes the way it is constructed, so be aware of the scenario: it is inappropriate when we want to expose a type to a customer but want to hide its data representation.

2) decimation device (Extrator)

An extractor is an object that defines the Unapply method. This method is called when a pattern match is made.
The Unapply method takes a data type and returns another data type, which means that the data of the parameter can be deconstructed into the returned data.

Like what
Class A
Class B (Val a:a)
Object TT {
def unapply (b:b) = Some (new A)
}
After this definition of the extractor TT, look at the pattern match: val B = new B (new A); b match{Case TT (a) = println (A)}
Intuitively thought to take B and TT type match, actually translated to tt.unapply (b) match{case Some (...) ...}

It also provides flexibility when compared to the case class above, which is equivalent to manually implementing unapply.
Follow-up will specifically introduce extrator, here first look at extractor how to achieve the case class can not achieve the "characterization of independence" (representation independence)

For example, the type we want to expose is a.

Defined as abstract type
Trait A

And then implement a specific subclass, with 2 construction parameters
Class B (Val p1:string, Val p2:string) extends A

Define a Extractor
Object mm{

The Apply method in the extractor is optional, here to facilitate the construction of an instance of a
def apply (p1:string, p2:string): A = new B (P1,P2);

Decompose a into (string,string)
def unapply (a:a): option[(String, string)] = {
if (A.isinstanceof[b]) {
Val B = A.asinstanceof[b]
Return Some (B.P1, B.P2)
}
None
}
}

This allows the customer to construct and pattern match only through MM (x, y). The customer only needs to deal with the factory/deconstruction role of MM, and the implementation of a is not affected.

Note:
There is a lot of information about the case class when it is often compared with the algebraic data class in the functional Language (ADT)
Strictly speaking, the case class in Scala is not ADT, but it is close enough to simulate ADT.
This article mentions that the case class is between the class inheritance and the algebraic data type http://blog.csdn.net/jinxfei/article/details/4677359
Mentions: "Scala provides a concept in between (class inheritance and algebraic data types), called conditional classes (case classes).

The Chinese version of programming in Scala mentions ADT in the Glossary:
A type that is defined by providing several alternates (alternative) that contain independent constructors. It is often possible to assist in the way the type is deconstructed through pattern matching.
This concept can be found in both the statute language and the functional language. Algebraic data types can be modeled in Scala using a sample class (case Class).

Transferred from: http://ifeve.com/pattern-matching-2/

Scala pattern matching 2 How is Scala implemented?

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.