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

What is the purpose of the Predef object of Scala

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

Share

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

This article mainly explains "What is the role of Scala's Predef object". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "What is the role of Scala's Predef object"!

Scala Predef Implicit Import

Scala automatically adds several implicit references to each program, just as Java programs automatically add java.lang packages.

in Scala. The contents of the following three packages are implicitly referenced in each program. The difference is. Scala also implicitly adds references to Predef. This is a generous way of doing the ape's job.

import java.lang._ // in JVM projects, or system namespace in .NETimport scala._ // everything in the scala packageimport Predef._ // everything in the Predef object

The above three packages include types and methods that are often used. The java.lang package includes frequently used java language types, which, assuming a. NET environment, reference the system namespace. Similarly, scala also implicitly references scala packages, i.e., introduces frequently used scala types.

Please note

The sequence of these three sentences hides something. We know that, usually, it is assumed that both packages imported have a definition of a certain type, for example, the same program. That's a reference to 'scala.collection.mutable.Set' and a reference to 'import scala.collection.immutable.Set' and the compiler says it can't decide which Set to use. Implicit references here are different, assuming the same type. The type of the next package hides the previous one. For example. StringBuilder is included in both java.lang and scala packages. In this case, the one defined in the scala package will be used. The definitions in java.lang are hidden unless they are shown using java. lang. StringBuilder.

Predef object Predef provides frequently used functions

The Predef object in package scala contains many useful methods. For example, if you write println in Scala source file, you actually call println of Predef. Predef.println instead calls Console.println, complete with real work.

def print(x: Any) = Console.print(x)def println() = Console.println()def println(x: Any) = Console.println(x)def printf(text: String, xs: Any*) = Console.print(text.format(xs: _*)

The assert function and related functions are also defined in Predef:

** Tests an expression, throwing an `AssertionError` if false.* Calls to this method will not be generated if `-Xelide-below`* is at least `ASSERTION`.** @see elidable* @param assertion the expression to test*/@elidable(ASSERTION)def assert(assertion: Boolean) {if (! assertion) throw new java.lang.AssertionError("assertion failed")}/** Tests an expression, throwing an `AssertionError` if false.* Calls to this method will not be generated if `-Xelide-below`* is at least `ASSERTION`.** @see elidable* @param assertion the expression to test* @param message a String to include in the failure message*/@elidable(ASSERTION) @inlinefinal def assert(assertion: Boolean, message: => Any) {if (! assertion) throw new java.lang.AssertionError("assertion failed: "+ message)}Predef defines a type alias

Predef is an object. In this object, define some type aliases. For example:

scala.collection.immutable.List // to force Nil, :: to be seen.type Function[-A, +B] = Function1[A, B]type Map[A, +B] = immutable.Map[A, B]type Set[A] = immutable.Set[A]val Map = immutable.Mapval Set = immutable.Set

Now we know. When using collections directly, such as List. Map。Set。The object in the immutable package is used. This is defined in Predef.

implicit conversion

Predef objects define implicit transformations that are often used, such as:

implicit final class any2stringadd[A](privateval self: A) extends AnyVal { def +(other: String): String = String.valueOf(self) + other}

Cain transformation. The +(other: String): String method is added to all subtypes of AnyVal to facilitate adding other value types when printing or other string operations are performed.

Another example:

@inline implicit def augmentString(x: String): StringOps = new StringOps(x)@inline implicit def unaugmentString(x: StringOps): String = x.repr

Implicit transformations allow us to freely use StringOps methods on Strings.

Similarly, rich wrappers for numeric types are implemented in this way.

Scala apes can care less about boxing and unpacking because the Predef object defines a direct implicit conversion between Scala value types and java primitive types.

implicit def byte2Byte(x: Byte) = java.lang.Byte.valueOf(x)implicit def short2Short(x: Short) = java.lang.Short.valueOf(x)implicit def char2Character(x: Char) = java.lang.Character.valueOf(x)implicit def int2Integer(x: Int) = java.lang.Integer.valueOf(x)implicit def long2Long(x: Long) = java.lang.Long.valueOf(x)implicit def float2Float(x: Float) = java.lang.Float.valueOf(x)implicit def double2Double(x: Double) = java.lang.Double.valueOf(x)implicit def boolean2Boolean(x: Boolean) = java.lang.Boolean.valueOf(x)implicit def Byte2byte(x: java.lang.Byte): Byte = x.byteValueimplicit def Short2short(x: java.lang.Short): Short = x.shortValueimplicit def Character2char(x: java.lang.Character): Char = x.charValueimplicit def Integer2int(x: java.lang.Integer): Int = x.intValueimplicit def Long2long(x: java.lang.Long): Long = x.longValueimplicit def Float2float(x: java.lang.Float): Float = x.floatValueimplicit def Double2double(x: java.lang.Double): Double = x.doubleValueimplicit def Boolean2boolean(x: java.lang.Boolean): Boolean = x.booleanValue

About Boxing and Unboxing

Readers familiar with languages such as Java or C#will know that boxing refers to converting primitive types into reference types (objects). For operations that require objects, unpacking converts objects to primitive types for scenes that require primitive types.

Because numeric types are class objects themselves, there is no need for boxing and unboxing in Scala.

Of course, Scala code will eventually execute on the JVM, so in practice, there will always be operations boxed into Scala class objects and unboxed into Java primitive value types, but these operations are transparent and the program does not have to care about them (in fact, this is done by implicit conversion defined in Predef).

At this point, I believe that everyone has a deeper understanding of "Scala's Predef object has what effect," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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