Scala Standard Library 2.13.15 - scala.StringContext.s (original) (raw)
Packages
- package root
This is the documentation for the Scala standard library.
This is the documentation for the Scala standard library.
Package structure
The scala package contains core types like Int, Float, Arrayor Option which are accessible in all Scala compilation units without explicit qualification or imports.
Notable packages include:
- scala.collection and its sub-packages contain Scala's collections framework
* scala.collection.immutable - Immutable, sequential data-structures such asVector, List,Range, HashMap orHashSet
* scala.collection.mutable - Mutable, sequential data-structures such asArrayBuffer,StringBuilder,HashMap or HashSet
* scala.collection.concurrent - Mutable, concurrent data-structures such asTrieMap - scala.concurrent - Primitives for concurrent programming such asFutures and Promises
- scala.io - Input and output operations
- scala.math - Basic math functions and additional numeric types likeBigInt and BigDecimal
- scala.sys - Interaction with other processes and the operating system
- scala.util.matching - Regular expressions
Other packages exist. See the complete list on the right.
Additional parts of the standard library are shipped as separate libraries. These include: - scala.reflect - Scala's reflection API (scala-reflect.jar)
- scala.xml - XML parsing, manipulation, and serialization (scala-xml.jar)
- scala.collection.parallel - Parallel collections (scala-parallel-collections.jar)
- scala.util.parsing - Parser combinators (scala-parser-combinators.jar)
- scala.swing - A convenient wrapper around Java's GUI framework called Swing (scala-swing.jar)
Automatic imports
Identifiers in the scala package and the scala.Predef object are always in scope by default.
Some of these identifiers are type aliases provided as shortcuts to commonly used classes. For example, List is an alias forscala.collection.immutable.List.
Other aliases refer to classes provided by the underlying platform. For example, on the JVM, String is an alias for java.lang.String.
Definition Classes
root
- package scala
Core Scala types.
Core Scala types. They are always available without an explicit import.
Definition Classes
root - case class StringContext(parts: String*) extends Product with Serializable
This class provides the basic mechanism to do String Interpolation.
This class provides the basic mechanism to do String Interpolation. String Interpolation allows users to embed variable references directly in *processed* string literals. Here's an example:
val name = "James"
println(s"Hello, $name") // Hello, James
Any processed string literal is rewritten as an instantiation and method call against this class. For example:
s"Hello, $name"
is rewritten to be:
StringContext("Hello, ", "").s(name)
By default, this class provides theraw,sandfmethods as available interpolators.
To provide your own string interpolator, create an implicit class which adds a method toStringContext. Here's an example:
implicit class JsonHelper(private val sc: StringContext) extends AnyVal {
def json(args: Any*): JSONObject = ...
}
val x: JSONObject = json"{ a: $a }"
Here theJsonHelperextension class implicitly adds thejsonmethod toStringContextwhich can be used forjsonstring literals.
parts
The parts that make up the interpolated string, without the expressions that get inserted by interpolation.
Definition Classes
scala - s
object s
Ordering
Alphabetic
By Inheritance
Hide All
Show All
Visibility
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
Test two objects for inequality.
Test two objects for inequality.
returnstrueif !(this == that), false otherwise.
Definition Classes
AnyRef → Any - final def ##: Int
Equivalent tox.hashCodeexcept for boxed numeric types andnull.
Equivalent tox.hashCodeexcept for boxed numeric types andnull. For numerics, it returns a hash value which is consistent with value equality: if two value type instances compare as true, then ## will produce the same hash value for each of them. Fornullreturns a hashcode wherenull.hashCodethrows aNullPointerException.
returns
a hash value consistent with ==
Definition Classes
AnyRef → Any - final def ==(arg0: Any): Boolean
The expressionx == thatis equivalent toif (x eq null) that eq null else x.equals(that).
The expressionx == thatis equivalent toif (x eq null) that eq null else x.equals(that).
returnstrueif the receiver object is equivalent to the argument;falseotherwise.
Definition Classes
AnyRef → Any - final def asInstanceOf[T0]: T0
Forces the compiler to treat the receiver object as having typeT0, even though doing so may violate type safety.
Forces the compiler to treat the receiver object as having typeT0, even though doing so may violate type safety.
This method is useful when you believe you have type information the compiler doesn't, and it also isn't possible to check the type at runtime. In such situations, skipping type safety is the only option.
It is platform dependent whetherasInstanceOfhas any effect at runtime. It might do a runtime type test on the erasure ofT0, insert a conversion (such as boxing/unboxing), fill in a default value, or do nothing at all.
In particular,asInstanceOfis not a type test. It does **not** mean:
this match {
case x: T0 => x
case _ => throw ClassCastException("...")
Use pattern matching or isInstanceOf for type testing instead.
Situations whereasInstanceOfis useful:- when flow analysis fails to deduce
T0automatically - when down-casting a type parameter or an abstract type member (which cannot be checked at runtime due to type erasure) If there is any doubt and you are able to type test instead, you should do so.
Be careful of usingasInstanceOfwhenT0is a primitive type. WhenT0is primitive,asInstanceOfmay insert a conversion instead of a type test. If your intent is to convert, use atoTmethod (x.toChar,x.toByte, etc.).
returns
the receiver object.
Definition Classes
Any
Exceptions thrown
ClassCastException if the receiver is not an instance of the erasure ofT0, if that can be checked on this platform
- when flow analysis fails to deduce
- def clone(): AnyRef
Create a copy of the receiver object.
Create a copy of the receiver object.
The default implementation of theclonemethod is platform dependent.
returns
a copy of the receiver object.
Attributes
protected[lang]
Definition Classes
AnyRef
Annotations
@throws(classOf[java.lang.CloneNotSupportedException]) @native()
Note
not specified by SLS as a member of AnyRef - final def eq(arg0: AnyRef): Boolean
Tests whether the argument (that) is a reference to the receiver object (this).
Tests whether the argument (that) is a reference to the receiver object (this).
Theeqmethod implements an equivalence relation on non-null instances ofAnyRef, and has three additional properties:- It is consistent: for any non-null instances
xandyof typeAnyRef, multiple invocations ofx.eq(y)consistently returnstrueor consistently returnsfalse. - For any non-null instance
xof typeAnyRef,x.eq(null)andnull.eq(x)returnsfalse. null.eq(null)returnstrue.
When overriding theequalsorhashCodemethods, it is important to ensure that their behavior is consistent with reference equality. Therefore, if two objects are references to each other (o1 eq o2), they should be equal to each other (o1 == o2) and they should hash to the same value (o1.hashCode == o2.hashCode).
returnstrueif the argument is a reference to the receiver object;falseotherwise.
Definition Classes
AnyRef
- It is consistent: for any non-null instances
- def equals(arg0: AnyRef): Boolean
The equality method for reference types.
The equality method for reference types. Default implementation delegates toeq.
See alsoequalsin scala.Any.
returnstrueif the receiver object is equivalent to the argument;falseotherwise.
Definition Classes
AnyRef → Any - def finalize(): Unit
Called by the garbage collector on the receiver object when there are no more references to the object.
Called by the garbage collector on the receiver object when there are no more references to the object.
The details of when and if thefinalizemethod is invoked, as well as the interaction betweenfinalizeand non-local returns and exceptions, are all platform dependent.
Attributes
protected[lang]
Definition Classes
AnyRef
Annotations
@throws(classOf[java.lang.Throwable])
Note
not specified by SLS as a member of AnyRef - final def getClass(): Class[_ <: AnyRef]
Returns the runtime class representation of the object.
Returns the runtime class representation of the object.
returns
a class object corresponding to the runtime type of the receiver.
Definition Classes
AnyRef → Any
Annotations
@native() - def hashCode(): Int
The hashCode method for reference types.
The hashCode method for reference types. See hashCode in scala.Any.
returns
the hash code value for this object.
Definition Classes
AnyRef → Any
Annotations
@native() - final def isInstanceOf[T0]: Boolean
Test whether the dynamic type of the receiver object has the same erasure asT0.
Test whether the dynamic type of the receiver object has the same erasure asT0.
Depending on whatT0is, the test is done in one of the below ways:
T0is a non-parameterized class type, e.g.BigDecimal: this method returnstrueif the value of the receiver object is aBigDecimalor a subtype ofBigDecimal.T0is a parameterized class type, e.g.List[Int]: this method returnstrueif the value of the receiver object is someList[X]for anyX. For example,List(1, 2, 3).isInstanceOf[List[String]]will return true.T0is some singleton typex.typeor literalx: this method returnsthis.eq(x). For example,x.isInstanceOf[1]is equivalent tox.eq(1)T0is an intersectionX with YorX & Y: this method is equivalent tox.isInstanceOf[X] && x.isInstanceOf[Y]``T0is a unionX | Y: this method is equivalent tox.isInstanceOf[X] || x.isInstanceOf[Y]T0is a type parameter or an abstract type member: this method is equivalent toisInstanceOf[U]whereUisT0's upper bound,AnyifT0is unbounded. For example,x.isInstanceOf[A]whereAis an unbounded type parameter will return true for any value ofx.
This is exactly equivalent to the type pattern_: T0
returnstrueif the receiver object is an instance of erasure of typeT0;falseotherwise.
Definition Classes
Any
Note
due to the unexpectedness ofList(1, 2, 3).isInstanceOf[List[String]]returning true andx.isInstanceOf[A]whereAis a type parameter or abstract member returning true, these forms issue a warning.
- final def ne(arg0: AnyRef): Boolean
Equivalent to!(this eq that).
Equivalent to!(this eq that).
returnstrueif the argument is not a reference to the receiver object;falseotherwise.
Definition Classes
AnyRef - final def notify(): Unit
Wakes up a single thread that is waiting on the receiver object's monitor.
Wakes up a single thread that is waiting on the receiver object's monitor.
Definition Classes
AnyRef
Annotations
@native()
Note
not specified by SLS as a member of AnyRef - final def notifyAll(): Unit
Wakes up all threads that are waiting on the receiver object's monitor.
Wakes up all threads that are waiting on the receiver object's monitor.
Definition Classes
AnyRef
Annotations
@native()
Note
not specified by SLS as a member of AnyRef - final def synchronized[T0](arg0: => T0): T0
Executes the code inbodywith an exclusive lock onthis.
Executes the code inbodywith an exclusive lock onthis.
returns
the result ofbody
Definition Classes
AnyRef - def toString(): String
Creates a String representation of this object.
Creates a String representation of this object. The default representation is platform dependent. On the java platform it is the concatenation of the class name, "@", and the object's hashcode in hexadecimal.
returns
a String representation of the object.
Definition Classes
AnyRef → Any - def unapplySeq(s: String): Option[Seq[String]]
The simple string matcher.
The simple string matcher.
Attempts to match the input string to the given interpolated patterns via a naive globbing, that is the reverse of the simple interpolator.
Here is an example usage:
val s"Hello, $name" = "Hello, James"
println(name) // "James"
In this example, the string "James" ends up matching the location where the pattern$nameis positioned, and thus ends up bound to that variable.
Multiple matches are supported:
val s"$greeting, $name" = "Hello, James"
println(greeting) // "Hello"
println(name) // "James"
And thesmatcher can match an arbitrary pattern within the${}block, for example:
val TimeSplitter = "([0-9]+).:".r
val s"The time is ${TimeSplitter(hours, mins)}" = "The time is 10.50"
println(hours) // 10
println(mins) // 50
Here, we use theTimeSplitterregex within thesmatcher, further splitting the matched string "10.50" into its constituent parts - final def wait(): Unit
See https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--.
Definition Classes
AnyRef
Annotations
@throws(classOf[java.lang.InterruptedException])
Note
not specified by SLS as a member of AnyRef - final def wait(arg0: Long, arg1: Int): Unit
See https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-
Definition Classes
AnyRef
Annotations
@throws(classOf[java.lang.InterruptedException])
Note
not specified by SLS as a member of AnyRef - final def wait(arg0: Long): Unit
See https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-.
Definition Classes
AnyRef
Annotations
@throws(classOf[java.lang.InterruptedException]) @native()
Note
not specified by SLS as a member of AnyRef
Ungrouped
Scala programming documentation. Copyright (c) 2002-2024 EPFL and Lightbend.