Implicit conversions to implicit in Scala

Source: Internet
Author: User

Implicit conversions in Scala are a very powerful language feature and can play two main roles:

1. Automatic implicit conversion of some data types

The string type cannot be automatically converted to the int type. Therefore, the compiler reports an error when a variable or constant of the int type is assigned a value of the string type. Therefore, the following statement is incorrect.

Val X: Int = "100"

To assign an integer value of the string type to the int type, for example, use the string. toint method, for example:

V Al x: Int = "100". toint

If you want to convert a string to an integer automatically, you can use implicit conversion. You can define the following functions.

Implicit def strtoint (STR: string) = Str. toint

When you assign a value to an int type variable, the string is automatically converted to an int type.

Scala> Val X: Int = "00"

X: Int = 100

If you define a function that adds two numbers at this time

Def add (X: int, Y: INT) = x + y

You can achieve this effect:

Scala> Add ("100", 200)

RES1: Int = 300

There are some rules for using implicit conversions, and there are two more important ones.

1. According to Scala programming, implicit conversions inserted must be in the scope in the form of a single identifier or associated with the source or target type of the conversion. Scala compiler will only consider implicit conversions in scope.

In short, before using implicit conversions, you need to use import to reference implicit conversions to the current scope or define implicit conversions in the scope. In addition to implicit conversions being introduced into the current scope, there is also a way to use implicit conversions, that is, the compiler will look for implicit definitions in the source type or expected companion object.

2. unambiguous rules: Implicit conversions can only be performed without other conversions. If more than one implicit conversion function is defined for the same source type in the same scope and multiple implicit conversion functions can be matched, the compiler reports an error, therefore, remove unnecessary implicit definitions during use.

Ii. Implicit Parameters

The Keri function has multiple parameter lists. To use default parameters for a parameter list, you can use the implicit parameter function provided by implicit. To do this, add implicit at the beginning of the parameter list to be automatically filled, define the default parameter value constant to be filled in the definition field, and declare implicit before the definition of the constant.

 

Horizon

When the definition is as follows:

Class container [A <% int] {def addit (X: a) = 123 + x}

Indicates that type A must be considered an int. Simply put, there is a need for a conversion function that can automatically convert the type to the int type. If there is no such conversion function, you can use implicit to define it.

 

 

References:

Scala programming Chapter 1

Twitter Scala school advanced type

Http://twitter.github.io/scala_school/zh_cn/advanced-types.html

 

Implicit conversions to implicit 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.