Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of scala implicit conversion

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the use of scala implicit conversion". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

I. Introduction

An implicit conversion from type S to type T is defined by an implicit value with function type S => T, or by an implicit method convertible to a value of that type. Implicit conversion applies in two cases:

1)If the expression e is of type S and S does not conform to the expected type T of the expression.

2), in the expression e.m with e of type S, if m does not denote a member of S

In the first case, search for a transformation c that applies to e and whose result type matches T. In the second case, the search for transformation c for e results in a member named m.

The following operations for the two lists xs and ys of list [Int] are legal:

xs Ordered[A]): Ordered[List[A]] =new Ordered[List[A]] { /* .. */ }

implicit def int2ordered(x: Int): Ordered[Int] =new Ordered[Int] { /* .. */ }

The implicitly imported object scala.Predef declares some predefined types (e.g. Pair) and methods (e.g. assert), as well as some implicit conversions. For example, when calling Java methods expecting java.lang.Integer, you are free to pass a scala.Int. This is because Predef contains the following implicit conversions:

import scala.language.implicitConversions

implicit def int2Integer(x: Int) =java.lang.Integer.valueOf(x)

Because implicit conversions can be flawed, the compiler issues warnings when compiling implicit conversion definitions if used indiscriminately.

To turn off warnings, do one of the following:

1), import scala.language. implicitConvertions into the scope of implicit conversion definitions

2)When calling the compiler, add: -language: implicitConvertions

When the compiler applies a transformation, no warning is issued.

Second, demo.

1, the demo of the first case

If you use it directly, you will report an error.

val i: Int = 1.5

Defining Implicit Transformation Methods

implicit def double2Int(d: Double) = d.toInt

It's normal to execute it again

2. Demo of the second case

class MYDF{ def show(sw : String) = println(sw)}object RDD2DF{ implicit def rdd2df(s : MYRDD) = new MYDF}class MYRDDobject AminalType extends App{ import yourpackage.RDD2DF._ val test = new MYRDD test.show("RDD converts into DF") }

The compiler does not have a show method when executing the test object. In this case, the compiler will look for implicit views in the scope that can make it compile. After finding the implicit conversion method of RDD2DF, it will perform implicit conversion first, and then call the show method.

3. Implicit transformation parameters

When defining a method, you can define the last parameter list as an implicit parameter. This is also widely used within spark, such as the previously published article on spark accumulator principle, custom accumulators and traps.

If the method has multiple implicit parameters, only an implicit modifier is required. When a method with implicit parameters is called, the compiler automatically populates the reorganization parameters with appropriate values if there are appropriate implicit values in the current context. The compiler throws an exception if there is none. Of course, we can also manually add default values for parameters marked as implicit.

def foo(n: Int)(implicit t1: String, t2: Double = 3.14)。

Missing the first step will result in a mistake.

Examples of this use in Spark include:

def accumulator[T](initialValue: T, name: String)(implicit param: AccumulatorParam[T]) : Accumulator[T] = { val acc = new Accumulator(initialValue, param, Some(name)) cleaner.foreach(_.registerAccumulatorForCleanup(acc)) acc}

III. Summary

Thoroughly understand the scala implicit conversion, we understand spark and spark-related product source code, such as mongodb, redis equal to spark combined with the implementation of source code principle has a crucial role.

"Scala implicit conversion usage" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report