Predef 对象提供了可以在所有 Scala 编译单元中访问的定义,而不需要显式限定。
Predef为常用的类型提供类型别名,例如不可变集合类型 scala.collection.immutable.Map, scala.collection.immutable.Set, and the scala.collection.immutable.List constructors (scala.collection.immutable.:: and scala.collection.immutable.Nil).
Predef 为控制台 I/O 提供了许多简单的函数,如 print、println、readLine、readInt 等。这些函数都是 scala.Console 提供的函数的别名。
提供了一组 assert 函数,用于记录和动态检查代码中的不变量。通过提供命令行选项 -Xdisable-assertions
,可以在编译时省略对断言的调用,该选项在 elidable.ASSERTION
引出- xlib。指向scalac命令。
还提供了用于静态分析工具的各种断言:assume
, require
and ensuring
. require
和 ensuring
用于作为函数的前置和后置条件的契约式设计样式规范的一种手段,目的是使这些规范能够被静态分析工具使用。例如,
def addNaturals(nats: List[Int]): Int = { require(nats forall (_ >= 0), "List contains negative numbers") nats.foldLeft(0)(_ + _) } ensuring(_ >= 0)
addNaturals 声明传递的整数列表应该只包含自然数(即非负数),并且返回的结果也应该是自然数。require 与 assert 的不同之处在于,如果条件失败,那么应该归咎于函数的调用者,而不是在 addNaturals 本身中发生的逻辑错误。ensure 是一种断言形式,它声明了函数所提供的关于其返回值的保证。
这里还定义了一些常用的隐式转换,以及父类型 scala.LowPriorityImplicits。为数值的“扩展”提供了隐式转换,例如,根据需要将短值转换为长值,并向数组值添加额外的高阶函数。这些在 scala.Array 的文档中有更详细的描述。
sealed abstract class <:<[-From, +To] extends (From) ⇒ To with Serializable
An instance of A <:< B
witnesses that A
is a subtype of B
.
sealed abstract class =:=[From, To] extends (From) ⇒ To with Serializable
An instance of A =:= B
witnesses that the types A
and B
are equal.
implicit final class ArrayCharSequence extends CharSequence
implicit final class ArrowAssoc[A] extends AnyVal
type Class[T] = java.lang.Class[T]
class DummyImplicit extends AnyRef
总是有一个隐式值的类型。
implicit final class Ensuring[A] extends AnyVal
type Function[-A, +B] = (A) ⇒ B
type Manifest[T] = reflect.Manifest[T]
type Map[A, +B] = collection.immutable.Map[A, B]
type OptManifest[T] = reflect.OptManifest[T]
implicit final class RichException extends AnyVal
implicit final class SeqCharSequence extends CharSequence
type Set[A] = collection.immutable.Set[A]
type String = java.lang.String
Scala 中的字符串类型有来自底层Java字符串的方法,也有通过scala.collection.immutable.StringOps隐式添加的方法。
implicit final class StringFormat[A] extends AnyVal
implicit final class any2stringadd[A] extends AnyVal
type ClassManifest[T] = ClassTag[T]
type Pair[+A, +B] = (A, B)
type Triple[+A, +B, +C] = (A, B, C)
implicit def $conforms[A]: <:<[A, A]
object =:= extends Serializable
def ???: Nothing
???
可用于标记仍有待实现的方法。
implicit def Boolean2boolean(x: java.lang.Boolean): Boolean
implicit def Byte2byte(x: java.lang.Byte): Byte
implicit def Character2char(x: Character): Char
implicit def Double2double(x: java.lang.Double): Double
object DummyImplicit
implicit def Float2float(x: java.lang.Float): Float
implicit def Integer2int(x: Integer): Int
implicit def Long2long(x: java.lang.Long): Long
val Manifest: ManifestFactory.type
val Map: collection.immutable.Map.type
val NoManifest: reflect.NoManifest.type
val Set: collection.immutable.Set.type
implicit def Short2short(x: java.lang.Short): Short
implicit val StringCanBuildFrom: CanBuildFrom[String, Char, String]
final def assert(assertion: Boolean, message: ⇒ Any): Unit
测试表达式,如果为假则抛出AssertionError。
def assert(assertion: Boolean): Unit
测试表达式,如果为假则抛出AssertionError。
final def assume(assumption: Boolean, message: ⇒ Any): Unit
测试表达式,如果为假则抛出AssertionError。
def assume(assumption: Boolean): Unit
测试表达式,如果为假则抛出AssertionError。
implicit def augmentString(x: String): StringOps
implicit def boolean2Boolean(x: Boolean): java.lang.Boolean
implicit def booleanArrayOps(xs: Array[Boolean]): ArrayOps[Boolean]
implicit def booleanWrapper(x: Boolean): RichBoolean
implicit def byte2Byte(x: Byte): java.lang.Byte
implicit def byteArrayOps(xs: Array[Byte]): ArrayOps[Byte]
implicit def byteWrapper(x: Byte): RichByte
我们更喜欢java.lang。*在任何潜在的冲突中,将类型装入这些包装器。
implicit def char2Character(x: Char): Character
implicit def charArrayOps(xs: Array[Char]): ArrayOps[Char]
implicit def charWrapper(c: Char): RichChar
def classOf[T]: Class[T]
检索类类型的运行时表示形式。
implicit def double2Double(x: Double): java.lang.Double
implicit def doubleArrayOps(xs: Array[Double]): ArrayOps[Double]
implicit def doubleWrapper(x: Double): RichDouble
implicit def fallbackStringCanBuildFrom[T]: CanBuildFrom[String, T, collection.immutable.IndexedSeq[T]]
implicit def float2Float(x: Float): java.lang.Float
implicit def floatArrayOps(xs: Array[Float]): ArrayOps[Float]
implicit def floatWrapper(x: Float): RichFloat
implicit def genericArrayOps[T](xs: Array[T]): ArrayOps[T]
implicit def genericWrapArray[T](xs: Array[T]): WrappedArray[T]
def identity[A](x: A): A
def implicitly[T](implicit e: T): T
implicit def int2Integer(x: Int): Integer
implicit def intArrayOps(xs: Array[Int]): ArrayOps[Int]
implicit def intWrapper(x: Int): RichInt
def locally[T](x: T): T
implicit def long2Long(x: Long): java.lang.Long
implicit def longArrayOps(xs: Array[Long]): ArrayOps[Long]
implicit def longWrapper(x: Long): RichLong
def manifest[T](implicit m: Manifest[T]): Manifest[T]
def optManifest[T](implicit m: OptManifest[T]): OptManifest[T]
def print(x: Any): Unit
def printf(text: String, xs: Any*): Unit
def println(x: Any): Unit
def println(): Unit
implicit def refArrayOps[T <: AnyRef](xs: Array[T]): ArrayOps[T]
final def require(requirement: Boolean, message: ⇒ Any): Unit
测试一个表达式,如果为假则抛出一个IllegalArgumentException。
def require(requirement: Boolean): Unit
测试一个表达式,如果为假则抛出一个IllegalArgumentException。
implicit def short2Short(x: Short): java.lang.Short
implicit def shortArrayOps(xs: Array[Short]): ArrayOps[Short]
implicit def shortWrapper(x: Short): RichShort
implicit def tuple2ToZippedOps[T1, T2](x: (T1, T2)): Ops[T1, T2]
implicit def tuple3ToZippedOps[T1, T2, T3](x: (T1, T2, T3)): Ops[T1, T2, T3]
implicit def unaugmentString(x: StringOps): String
implicit def unitArrayOps(xs: Array[Unit]): ArrayOps[Unit]
implicit def unwrapString(ws: WrappedString): String
implicit def wrapBooleanArray(xs: Array[Boolean]): WrappedArray[Boolean]
implicit def wrapByteArray(xs: Array[Byte]): WrappedArray[Byte]
implicit def wrapCharArray(xs: Array[Char]): WrappedArray[Char]
implicit def wrapDoubleArray(xs: Array[Double]): WrappedArray[Double]
implicit def wrapFloatArray(xs: Array[Float]): WrappedArray[Float]
implicit def wrapIntArray(xs: Array[Int]): WrappedArray[Int]
implicit def wrapLongArray(xs: Array[Long]): WrappedArray[Long]
implicit def wrapRefArray[T <: AnyRef](xs: Array[T]): WrappedArray[T]
implicit def wrapShortArray(xs: Array[Short]): WrappedArray[Short]
implicit def wrapString(s: String): WrappedString
implicit def wrapUnitArray(xs: Array[Unit]): WrappedArray[Unit]
val ClassManifest: ClassManifestFactory.type
object Pair
object Triple
def any2ArrowAssoc[A](x: A): ArrowAssoc[A]
def any2Ensuring[A](x: A): Ensuring[A]
def any2stringfmt(x: Any): StringFormat[Any]
def arrayToCharSequence(xs: Array[Char]): CharSequence
def classManifest[T](implicit m: ClassManifest[T]): ClassManifest[T]
def conforms[A]: <:<[A, A]
def error(message: String): Nothing
def exceptionWrapper(exc: scala.Throwable): RichException
def readBoolean(): Boolean
def readByte(): Byte
def readChar(): Char
def readDouble(): Double
def readFloat(): Float
def readInt(): Int
def readLine(text: String, args: Any*): String
def readLine(): String
def readLong(): Long
def readShort(): Short
def readf(format: String): List[Any]
def readf1(format: String): Any
def readf2(format: String): (Any, Any)
def readf3(format: String): (Any, Any, Any)
def seqToCharSequence(xs: collection.IndexedSeq[Char]): CharSequence
原文地址:https://www.scala-lang.org/api/2.11.12/#scala.Predef$