Movatterモバイル変換


[0]ホーム

URL:


Scala 3
3.7.4
LearnInstallPlaygroundFind A LibraryCommunityBlog
Scala 3
LearnInstallPlaygroundFind A LibraryCommunityBlog
DocsAPI
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL
Scala 3/scala/Predef

Predef

scala.Predef
objectPredef

ThePredef object provides definitions that are accessible in all Scala compilation units without explicit qualification.

Commonly Used Types

Predef provides type aliases for types which are commonly used, such as the immutable collection typesscala.collection.immutable.Map andscala.collection.immutable.Set.

Console Output

For basic console output,Predef provides convenience methodsprint andprintln, which are aliases of the methods in the objectscala.Console.

Assertions

A set ofassert functions are provided for use as a way to document and dynamically check invariants in code. Invocations ofassert can be elided at compile time by providing the command line option-Xdisable-assertions, which raises-Xelide-below aboveelidable.ASSERTION, to thescalac command.

Variants ofassert intended for use with static analysis tools are also provided:assume,require andensuring.require andensuring are intended for use as a means of design-by-contract style specification of pre- and post-conditions on functions, with the intention that these specifications could be consumed by a static analysis tool. For instance,

def addNaturals(nats: List[Int]): Int = {  require(nats forall (_ >= 0), "List contains negative numbers")  nats.foldLeft(0)(_ + _)} ensuring(_ >= 0)

The declaration ofaddNaturals states that the list of integers passed should only contain natural numbers (i.e. non-negative), and that the result returned will also be natural.require is distinct fromassert in that if the condition fails, then the caller of the function is to blame rather than a logical error having been made withinaddNaturals itself.ensuring is a form ofassert that declares the guarantee the function is providing with regards to its return value.

Implicit Conversions

A number of commonly applied implicit conversions are also defined here, and in the parent typescala.LowPriorityImplicits. Implicit conversions are provided for the "widening" of numeric values, for instance, converting a Short value to a Long value as required, and to add additional higher-order functions to Array values. These are described in more detail in the documentation ofscala.Array.

Attributes

Source
Predef.scala
Graph
Supertypes
classObject
traitMatchable
classAny
Self type
Predef.type

Members list

Grouped members

Utility Methods

??? can be used for marking methods that remain to be implemented.

??? can be used for marking methods that remain to be implemented.

Attributes

Throws

NotImplementedErrorwhen??? is invoked.

Source
Predef.scala
defclassOf[T]:Class[T]

Retrieve the runtime representation of a class type.

Retrieve the runtime representation of a class type.classOf[T] is equivalent to the class literalT.class in Java.

Attributes

Returns

The runtimeClass representation of typeT.

Example

val listClass = classOf[List[_]]// listClass is java.lang.Class[List[_]] = class scala.collection.immutable.Listval mapIntString = classOf[Map[Int,String]]// mapIntString is java.lang.Class[Map[Int,String]] = interface scala.collection.immutable.Map
Source
Predef.scala
defidentity[A](x:A):A

A method that returns its input value.

A method that returns its input value.

Type parameters

A

type of the input value x.

Value parameters

x

the value of typeA to be returned.

Attributes

Returns

the valuex.

Source
Predef.scala
defimplicitly[T](implicite:T):T

Summon an implicit value of typeT.

Summon an implicit value of typeT. Usually, the argument is not passed explicitly.

Type parameters

T

the type of the value to be summoned

Attributes

Returns

the implicit value of typeT

Source
Predef.scala
deflocally[T](x:T):T

Used to mark code blocks as being expressions, instead of being taken as part of anonymous classes and the like.

Used to mark code blocks as being expressions, instead of being taken as part of anonymous classes and the like. This is just a different name foridentity.

Attributes

Example

Separating code blocks fromnew:

val x = new AnyRef{  val y = ...  println(y)}// the { ... } block is seen as the body of an anonymous classval x = new AnyRef{  val y = ...  println(y)}// an empty line is a brittle "fix"val x = new AnyReflocally {  val y = ...  println(y)}// locally guards the block and helps communicate intent
Source
Predef.scala
defvalueOf[T](implicitvt:ValueOf[T]):T

Retrieve the single value of a type with a unique inhabitant.

Retrieve the single value of a type with a unique inhabitant.

Attributes

Example

object Fooval foo = valueOf[Foo.type]// foo is Foo.type = Fooval bar = valueOf[23]// bar is 23.type = 23
Source
Predef.scala

Assertions

These methods support program verification and runtime correctness.

defassert(assertion:Boolean):Unit

Tests an expression, throwing anAssertionError if false.

Tests an expression, throwing anAssertionError if false. Calls to this method will not be generated if-Xelide-below is greater thanASSERTION.

Value parameters

assertion

the expression to test

Attributes

See also
Source
Predef.scala
finaldefassert(assertion:Boolean,message:=>Any):Unit

Tests an expression, throwing anAssertionError if false.

Tests an expression, throwing anAssertionError if false. Calls to this method will not be generated if-Xelide-below is greater thanASSERTION.

Value parameters

assertion

the expression to test

message

a String to include in the failure message

Attributes

See also
Source
Predef.scala
defassume(assumption:Boolean):Unit

Tests an expression, throwing anAssertionError if false.

Tests an expression, throwing anAssertionError if false. This method differs from assert only in the intent expressed: assert contains a predicate which needs to be proven, while assume contains an axiom for a static checker. Calls to this method will not be generated if-Xelide-below is greater thanASSERTION.

Value parameters

assumption

the expression to test

Attributes

See also
Source
Predef.scala
finaldefassume(assumption:Boolean,message:=>Any):Unit

Tests an expression, throwing anAssertionError if false.

Tests an expression, throwing anAssertionError if false. This method differs from assert only in the intent expressed: assert contains a predicate which needs to be proven, while assume contains an axiom for a static checker. Calls to this method will not be generated if-Xelide-below is greater thanASSERTION.

Value parameters

assumption

the expression to test

message

a String to include in the failure message

Attributes

See also
Source
Predef.scala
defrequire(requirement:Boolean):Unit

Tests an expression, throwing anIllegalArgumentException if false.

Tests an expression, throwing anIllegalArgumentException if false. This method is similar toassert, but blames the caller of the method for violating the condition.

Value parameters

requirement

the expression to test

Attributes

Source
Predef.scala
finaldefrequire(requirement:Boolean,message:=>Any):Unit

Tests an expression, throwing anIllegalArgumentException if false.

Tests an expression, throwing anIllegalArgumentException if false. This method is similar toassert, but blames the caller of the method for violating the condition.

Value parameters

message

a String to include in the failure message

requirement

the expression to test

Attributes

Source
Predef.scala

Console Output

These methods provide output via the console.

defprint(x:Any):Unit

Prints an object toout using itstoString method.

Prints an object toout using itstoString method.

Value parameters

x

the object to print; may be null.

Attributes

Source
Predef.scala
defprintf(text:String,xs:Any*):Unit

Prints its arguments as a formatted string to the default output, based on a string pattern (in a fashion similar to printf in C).

Prints its arguments as a formatted string to the default output, based on a string pattern (in a fashion similar to printf in C).

The interpretation of the formatting patterns is described injava.util.Formatter.

Consider using thef interpolator as more type safe and idiomatic.

Value parameters

text

the pattern for formatting the arguments.

xs

the arguments used to instantiate the pattern.

Attributes

Throws

java.lang.IllegalArgumentException if there was a problem with the format string or arguments

See also
Source
Predef.scala

Prints a newline character on the default output.

Prints a newline character on the default output.

Attributes

Source
Predef.scala

Prints out an object to the default output, followed by a newline character.

Prints out an object to the default output, followed by a newline character.

Value parameters

x

the object to print.

Attributes

Source
Predef.scala

Aliases

These aliases bring selected immutable types into scope without any imports.

val->:Tuple2.type

Allows destructuring tuples with the same syntax as constructing them.

Allows destructuring tuples with the same syntax as constructing them.

Attributes

Example

val tup = "foobar" -> 3val c = tup match { case str -> i => str.charAt(i)}
Source
Predef.scala
typeClass[T] =Class[T]

Attributes

Source
Predef.scala
typeFunction[-A,+B] =A=>B

Attributes

Source
Predef.scala
typeMap[K,+V] =Map[K,V]

Attributes

Source
Predef.scala
valMap:Map.type

Attributes

Source
Predef.scala
typeSet[A] =Set[A]

Attributes

Source
Predef.scala
valSet:Set.type

Attributes

Source
Predef.scala

TheString type in Scala has all the methods of the underlyingjava.lang.String, of which it is just an alias.

TheString type in Scala has all the methods of the underlyingjava.lang.String, of which it is just an alias.

In addition, extension methods inscala.collection.StringOps are added implicitly through the conversionaugmentString.

Attributes

Source
Predef.scala

String Conversions

Conversions from String to StringOps or WrappedString.

Attributes

Source
Predef.scala

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala

Implicit Classes

These implicit classes add useful extension methods to every type.

final implicitclassArrowAssoc[A](self:A) extendsAnyVal

Attributes

Source
Predef.scala
Supertypes
classAnyVal
traitMatchable
classAny
final implicitdefArrowAssoc[A](self:A):ArrowAssoc[A]

Attributes

Source
Predef.scala
final implicitclassEnsuring[A](self:A) extendsAnyVal

Attributes

Source
Predef.scala
Supertypes
classAnyVal
traitMatchable
classAny
final implicitdefEnsuring[A](self:A):Ensuring[A]

Attributes

Source
Predef.scala
final implicitclassStringFormat[A](self:A) extendsAnyVal

Attributes

Source
Predef.scala
Supertypes
classAnyVal
traitMatchable
classAny
final implicitdefStringFormat[A](self:A):StringFormat[A]

Attributes

Source
Predef.scala
final implicitclassany2stringadd[A](self:A) extendsAnyVal

Injects String concatenation operator+ to any classes.

Injects String concatenation operator+ to any classes.

Attributes

Deprecated
[Since version 2.13.0]Implicit injection of + is deprecated. Convert to String to call +
Source
Predef.scala
Supertypes
classAnyVal
traitMatchable
classAny
final implicitdefany2stringadd[A](self:A):any2stringadd[A]

Injects String concatenation operator+ to any classes.

Injects String concatenation operator+ to any classes.

Attributes

Deprecated
[Since version 2.13.0]Implicit injection of + is deprecated. Convert to String to call +
Source
Predef.scala

CharSequence Wrappers

Wrappers that implements CharSequence and were implicit classes.

finalclassArrayCharSequence(arrayOfChars:Array[Char]) extendsCharSequence

Attributes

Source
Predef.scala
Supertypes
classObject
traitMatchable
classAny

Attributes

Source
Predef.scala
finalclassSeqCharSequence(sequenceOfChars:IndexedSeq[Char]) extendsCharSequence

Attributes

Source
Predef.scala
Supertypes
classObject
traitMatchable
classAny

Attributes

Source
Predef.scala

Java to Scala

Implicit conversion from Java primitive wrapper types to Scala equivalents.

Attributes

Source
Predef.scala
implicitdefByte2byte(x:Byte):Byte

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala
implicitdefFloat2float(x:Float):Float

Attributes

Source
Predef.scala
implicitdefInteger2int(x:Integer):Int

Attributes

Source
Predef.scala
implicitdefLong2long(x:Long):Long

Attributes

Source
Predef.scala
implicitdefShort2short(x:Short):Short

Attributes

Source
Predef.scala

Scala to Java

Implicit conversion from Scala AnyVals to Java primitive wrapper types equivalents.

Attributes

Source
Predef.scala
implicitdefbyte2Byte(x:Byte):Byte

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala
implicitdeffloat2Float(x:Float):Float

Attributes

Source
Predef.scala
implicitdefint2Integer(x:Int):Integer

Attributes

Source
Predef.scala
implicitdeflong2Long(x:Long):Long

Attributes

Source
Predef.scala
implicitdefshort2Short(x:Short):Short

Attributes

Source
Predef.scala

Array to ArraySeq

Conversions from Arrays to ArraySeqs.

implicitdefgenericWrapArray[T](xs:Array[T]):ArraySeq[T]

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala
implicitdefwrapByteArray(xs:Array[Byte]):ofByte

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala
implicitdefwrapCharArray(xs:Array[Char]):ofChar

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala
implicitdefwrapIntArray(xs:Array[Int]):ofInt

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala
implicitdefwrapLongArray(xs:Array[Long]):ofLong

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala
implicitdefwrapRefArray[T <:AnyRef](xs:Array[T]):ofRef[T]

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala
implicitdefwrapUnitArray(xs:Array[Unit]):ofUnit

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala

Type members

Types

typeManifest[T] =Manifest[T]

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala
infixtypeis[A <:AnyKind,B] =B {typeSelf =A; }

A type supporting Self-based type classes.

A type supporting Self-based type classes.

A is TC

expands to

TC { type Self = A }

which is what is needed for a context bound[A: TC].

Attributes

Source
Predef.scala

Value members

Concrete methods

defmanifest[T](implicitm:Manifest[T]):Manifest[T]

Attributes

Source
Predef.scala
defoptManifest[T](implicitm:OptManifest[T]):OptManifest[T]

Attributes

Source
Predef.scala
transparent inlinedefsummon[T](usingx:T): x.type

Summon a given value of typeT. Usually, the argument is not passed explicitly.

Summon a given value of typeT. Usually, the argument is not passed explicitly.

Type parameters

T

the type of the value to be summoned

Attributes

Returns

the given value typed: the provided type parameter

Source
Predef.scala

Concrete fields

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala

Extensions

Extensions

extension[T](x:T |Null)
inlinedefnn: x.type &T

Strips away the nullability from a value. Note that.nn performs a checked cast, so if invoked on anull value it will throw anNullPointerException.

Strips away the nullability from a value. Note that.nn performs a checked cast, so if invoked on anull value it will throw anNullPointerException.

Attributes

Example
val s1: String | Null = "hello"val s2: String = s1.nnval s3: String | Null = nullval s4: String = s3.nn // throw NullPointerException
Source
Predef.scala
extension(inlinex:AnyRef |Null)
infix inlinedefeq(inliney:AnyRef |Null):Boolean

Enables an expression of typeT|Null, whereT is a subtype ofAnyRef, to be checked fornull usingeq rather than only==. This is needed becauseNull no longer haseq orne methods, only== and!= inherited fromAny.

Enables an expression of typeT|Null, whereT is a subtype ofAnyRef, to be checked fornull usingeq rather than only==. This is needed becauseNull no longer haseq orne methods, only== and!= inherited fromAny.

Attributes

Source
Predef.scala
infix inlinedefne(inliney:AnyRef |Null):Boolean

Enables an expression of typeT|Null, whereT is a subtype ofAnyRef, to be checked fornull usingne rather than only!=. This is needed becauseNull no longer haseq orne methods, only== and!= inherited fromAny.

Enables an expression of typeT|Null, whereT is a subtype ofAnyRef, to be checked fornull usingne rather than only!=. This is needed becauseNull no longer haseq orne methods, only== and!= inherited fromAny.

Attributes

Source
Predef.scala

Experimental extensions

extension(opt:Option.type)
inlinedeffromNullable[T](t:T |Null):Option[T]

Attributes

Experimental
true
Source
Predef.scala
extension[T](x:T)
inlinedefruntimeChecked: x.type

Asserts that a term should be exempt from static checks that can be reliably checked at runtime.

Asserts that a term should be exempt from static checks that can be reliably checked at runtime.

Attributes

Example
val xs: Option[Int] = Option(1)xs.runtimeChecked match  case Some(x) => x // `Some(_)` can be checked at runtime, so no warning
val xs: List[Int] = List(1,2,3)val y :: ys = xs.runtimeChecked // `_ :: _` can be checked at runtime, so no warning
Experimental
true
Source
Predef.scala

Implicits

Implicits

implicitdef$conforms[A]:A=>A

An implicit of typeA => A is available for allA because it can always be implemented using the identity function.

An implicit of typeA => A is available for allA because it can always be implemented using the identity function. This also means that an implicit of typeA => B is always available whenA <: B, because(A => A) <: (A => B).

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala
implicitdefgenericArrayOps[T](xs:Array[T]):ArrayOps[T]

Attributes

Source
Predef.scala
implicitdefintArrayOps(xs:Array[Int]):ArrayOps[Int]

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala
implicitdefrefArrayOps[T <:AnyRef](xs:Array[T]):ArrayOps[T]

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala
implicitdeftuple2ToZippedOps[T1,T2](x: (T1,T2)):Ops[T1,T2]

Attributes

Source
Predef.scala
implicitdeftuple3ToZippedOps[T1,T2,T3](x: (T1,T2,T3)):Ops[T1,T2,T3]

Attributes

Source
Predef.scala

Attributes

Source
Predef.scala

Inherited implicits

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala
implicitdefbyteWrapper(x:Byte):RichByte

We prefer the java.lang.* boxed types to these wrappers in any potential conflicts.

We prefer the java.lang.* boxed types to these wrappers in any potential conflicts. Conflicts do exist because the wrappers need to implement ScalaNumber in order to have a symmetric equals method, but that implies implementing java.lang.Number as well.

Note - these are inlined because they are value classes, but the call to xxxWrapper is not eliminated even though it does nothing. Even inlined, every call site does a no-op retrieval of Predef's MODULE$ because maybe loading Predef has side effects!

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala
implicitdefcharWrapper(c:Char):RichChar

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala
implicitdefintWrapper(x:Int):RichInt

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala
implicitdeflongWrapper(x:Long):RichLong

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala

Attributes

Inherited from:
LowPriorityImplicits (hidden)
Source
Predef.scala

Deprecated and Inherited implicits

Attributes

Deprecated
[Since version 2.13.0]implicit conversions from Array to immutable.IndexedSeq are implemented by copying; use `toIndexedSeq` explicitly if you want to copy, or use the more efficient non-copying ArraySeq.unsafeWrapArray
Inherited from:
LowPriorityImplicits2 (hidden)
Source
Predef.scala
In this article
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL

[8]ページ先頭

©2009-2025 Movatter.jp