Programming in Scala (Second Edition) reading notes 21 implicit conversions

Source: Internet
Author: User

1.

There ' s a fundamental difference between your own code and libraries of

Other people:you can change or extend your own code as you wish, but if

You want to use someone else's libraries, you usually has to take them as

They is.

You can change or expand your own code, but the code for others can only be accepted as it is.

2.

A number of constructs has sprung up and programming languages to

Alleviate this problem. Ruby has modules, and Smalltalk lets packages add

To all other ' s classes. These is very powerful, but also dangerous

You modify the behavior of a class for an entire application, some parts of

Which might not know

Programming languages have developed many ways to solve this problem. Ruby has module, Smalltalk ... , these methods are powerful and dangerous. You change the behavior of a class, which has an impact on the entire application. Some of the effects are unpredictable.

3.

C # 3.0 has static extension methods, which is

More local, but also more restrictive in the can only add methods, not

Fields, to a class, and your can ' t make a class implement new interfaces

C#3.0 allows you to add a new method to a class, but you cannot add a field or make the class implement a new interface

4.Scala ' s answer is implicit conversions and parameters

Scala's answer is implicit conversions and implicit arguments


5. Typical applications for implicit conversions

Before delving into the details of implicit conversions, take a look at a typical

Example of their use. Implicit conversions is often helpful for working with

The bodies of software that were developed without each of the other in mind. each

Library has its own-encode a concept that's essentially the same thing.

Implicit conversions help by reducing the number of explicit conversions that

is needed from one type to another.

Each package has its own way of representing a concept. The concepts expressed by different methods in different packets may be the same in nature. This can then be converted with implicit


6. Example 1

The ActionListener of the Java swing package is an interface with only one method. This is how Java implements functions similar to functions.

Val button = new JButton Button.addactionlistener (new ActionListener {def actionperformed (event:actionevent) {      println ("pressed!") }    })

Scala can pass a function directly to the addActionListener method

Button.addactionlistener (//Type mismatch! (_: ActionEvent) = println ("pressed!"))

But there is a type mismatch, and we need to define a conversion method that will allow the compiler to do the conversion automatically.

Implicit def function2actionlistener (f:actionevent = Unit) =new ActionListener {def actionperformed (event: ActionEvent) = f (Event)}

The conversion process of the compiler

The the-this-code works is and the compiler first tries to compile it's, but

It sees a type error. before giving up, it looks for a implicit conversion that

Can repair the problem. In this case, it finds Function2actionlistener. It

tries that conversion method, sees the it works, and moves on. The compiler

Works hard, so, the developer can ignore one more fiddly detail.

Action listener? Action event function? Either one would work-use the one

That's more convenient.


7. Rules for implicit conversions

Implicit definitions is those, the compiler is allowed to insert a

Program in order to fix any of its type errors. For example, if x + y does

Not type check, then the compiler might change it to convert (x) + Y, where

Convert is some available implicit conversion. If Convert changes x into

something that have a + method, then the change might fix a

Type checks and runs correctly.

If X does not have a + this method, it attempts to convert the type of x. Why convert x without converting y? Because + is the left associative operator

There is also a typical implicit conversion of Stream's #:: Method.


Marking rule:only definitions marked implicit is available

Only methods marked as implicit can be used for implicit conversions.


Scope Rule:an inserted implicit conversion must be in scope as a single

identifier, or is associated with the source or target type of the conversion

1. The single-identifier function within the scope is only possible. Abc.def such a function is not possible because it is not a single identifier.

2. Find out if an implicit conversion function is defined in the companion object associated with the source type. For example

Object Steam {/** A wrapper method that adds ' #:: ' for Cons and ' #::: For Concat as operations * to streams. */implicit def Conswrapper[a] (stream: + stream[a]): conswrapper[a] = new Conswrapper[a] (stream)}


Naming an implicit conversion. Implicit conversions can have arbitrary

Names. It doesn't matter if you take a name





Programming in Scala (Second Edition) reading notes 21 implicit conversions

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.