Scala programming implicit conversions and implicit parameters (ix)

Source: Internet
Author: User
Tags ticket
Scala Programming implicit conversion and implicit parameter introduction

Scala offers an implicit conversion and implicit parameter functionality that is very feature-specific. is a feature that is not in programming languages such as Java. It allows you to manually specify that an object of some type be converted to another type of object. With these features, you can achieve very powerful, and special features.

In Scala's implicit conversion, the most important thing is to define the implicit conversion function, that is, implicit conversion functions. The implicit conversion function that is defined is automatically used by Scala as long as it is introduced within the programmed program. Scala will automatically pass in an implicit conversion function, converting it to an object of another type and returning it, based on the signature of the implicit conversion function, when using the object defined by the parameter type that the implicit conversion function receives in the program. This is an implicit conversion.

It does not matter what the implicit conversion function is called because it is not usually called manually by the user, but is called by Scala. However, if you want to use an implicit conversion, you need to import the implicit conversion function. It is therefore generally recommended that the name of the implicit conversion function be named "One2One".

There are a number of implicit conversions and implicit parameters in the spark source code, so you must be proficient in this syntax. Implicit conversions

To implement an implicit conversion, you can define an implicit conversion function as long as the program is visible within the scope. Scala automatically uses an implicit conversion function. The only syntactic difference between an implicit conversion function and a normal function is that it starts with implicit, and it is best to define the function return type.

//Case: Special Ticketing window (only for special groups such as students, seniors, etc.)
class Specialperson (Val name:string)
class Student (Val name:string)
class older (Val name:string)

implicit def Object2specialperson (obj:object): Specialperson = {
  if (obj.get Class = = Classof[student]) {val stu = obj.asinstanceof[student]; new Specialperson (Stu.name)}
  else if (obj.getclass = = Classof[older]) {val older = Obj.asinstanceof[older]; new Specialperson (Older.name)}
  else Nil
}

var tick Etnumber = 0
def buyspecialticket (P:specialperson) = {
  Ticketnumber + = 1
  "T" + ticketnumber
}
using implicit conversions to enforce existing types
One of the most powerful features of implicit conversions is the ability to enhance the functionality of existing types unconsciously. That is, you can define a reinforced class for a class and define implicit conversions to each other, so that the source class is automatically converted to the enhanced class by Scala when using the enhanced version of the method, and then the method is called.

//Case: Superman Change

class man (Val name:string)
class Superman (Val name:string) {
  def emitlaser = println ("Emit a laster! ")
}

Implicit def Man2superman (Man:man): Superman = new Superman (Man.name)

val leo = new Man ("Leo")
Leo.emitlaser
Implicit conversion function scope and import
Scala uses two implicit conversions by default, a source type, or an implicit conversion function within a companion object of the target type, and an implicit conversion function that can be represented in the current program scope with a unique identifier.

//If the implicit conversion function is not in either of these cases, you must manually use import syntax to introduce an implicit conversion function under a package, such as import test._. It is generally recommended to use Iimport to import implicit conversion functions only where implicit conversions are required, such as in a function or method, to reduce the scope of the implicit conversion function and avoid unwanted implicit conversions.
timing of implicit conversions
1. Call a function, but the type of the parameter passed to the function does not match the type of the receive parameter defined by the function (case: Special ticket window)
//2, use an object of a certain type, call a method, and this method does not exist in the type (case: Superman transformation)
//3, A method is called using an object of a type, although the type has this method, but the type of the parameter passed to the method does not match the type of the method-defined receive parameter (Case: Special ticket window)

//Case: Special Ticket Window Enhanced Edition
class Tickethouse {
  var ticketnumber = 0
  def buyspecialticket (P:specialperson) = {
    Ticketnumber + = 1
    "T" + tic Ketnumber
  }
}
implicitly-parameter
The so-called implicit parameter, which refers to a function or method, defines a parameter modified with implicit, when Scala attempts to find a specified type of implicit-decorated object, that is, an implicit value, and injects parameters.
//Scala looks in two scopes: one is the implicit variable defined by Val or var that is visible within the current scope, and one is an implicit value within the associated object of the implicit parameter type

//Case: Exam Sign-in
class Signpen {
  def write (content:string) = println (content)
}
implicit val signpen = new Signpen

def signforexam (name:s Tring) (implicit signpen:signpen) {
  signpen.write (name + "come to exam in time.")
}

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.