![]() |
想出国的碗 · 斗罗大陆之催眠武魂最新章节列表,斗罗大陆之催 ...· 3 月前 · |
![]() |
知识渊博的单车 · 最高人民法院最高人民检察院司法解释目前明确规 ...· 3 月前 · |
![]() |
有腹肌的李子 · 翟天临硕士论文抄袭学位被取消!你的论文抄了吗?· 4 月前 · |
![]() |
淡定的香烟 · 为什么没有高手对迈克尔杰克逊的声音唱功做个详 ...· 5 月前 · |
This is the documentation for the Scala standard library.
Core Scala types.
Core Scala types. They are always available without an explicit import.
This package object contains primitives for concurrent and parallel programming.
This package object contains primitives for concurrent and parallel programming.
A more detailed guide to Futures and Promises, including discussion and examples can be found at https://docs.scala-lang.org/overviews/core/futures.html .
When working with Futures, you will often find that importing the whole concurrent package is convenient:
import scala.concurrent._
When using things like
Future
s, it is often required to have an implicit
ExecutionContext
in scope. The general advice for these implicits are as follows.
If the code in question is a class or method definition, and no
ExecutionContext
is available,
request one from the caller by adding an implicit parameter list:
def myMethod(myParam: MyType)(implicit ec: ExecutionContext) = … class MyClass(myParam: MyType)(implicit ec: ExecutionContext) { … }
This allows the caller of the method, or creator of the instance of the class, to decide which
ExecutionContext
should be used.
For typical REPL usage and experimentation, importing the global
ExecutionContext
is often desired.
import scala.concurrent.ExcutionContext.Implicits.global
Operations often require a duration to be specified. A duration DSL is available to make defining these easier:
import scala.concurrent.duration._ val d: Duration = 10.seconds
Basic use of futures is easy with the factory method on Future, which executes a provided function asynchronously, handing you back a future result of that function without blocking the current thread. In order to create the Future you will need either an implicit or explicit ExecutionContext to be provided:
import scala.concurrent._ import ExecutionContext.Implicits.global // implicit execution context val firstZebra: Future[Int] = Future { val words = Files.readAllLines("/etc/dictionaries-common/words").asScala words.indexOfSlice("zebra") }
Although blocking is possible in order to await results (with a mandatory timeout duration):
import scala.concurrent.duration._ Await.result(firstZebra, 10.seconds)
and although this is sometimes necessary to do, in particular for testing purposes, blocking in general is discouraged when working with Futures and concurrency in order to avoid potential deadlocks and improve performance. Instead, use callbacks or combinators to remain in the future domain:
val animalRange: Future[Int] = for { aardvark <- firstAardvark zebra <- firstZebra } yield zebra - aardvark animalRange.onSuccess { case x if x > 500000 => println("It's a long way from Aardvark to Zebra") }
The jdk package contains utilities to interact with JDK classes.
The jdk package contains utilities to interact with JDK classes.
This packages offers a number of converters, that are able to wrap or copy types from the scala library to equivalent types in the JDK class library and vice versa:
By convention, converters that wrap an object to provide a different interface to the same underlying data structure use .asScala and .asJava extension methods, whereas converters that copy the underlying data structure use .toScala and .toJava.
In the javaapi package, the same converters can be found with a java-friendly interface that don't rely on implicit enrichments.
Additionally, this package offers Accumulator s, capable of efficiently traversing JDK Streams.
The package object
scala.math
contains methods for performing basic
numeric operations such as elementary exponential, logarithmic, root and
trigonometric functions.
The package object
scala.math
contains methods for performing basic
numeric operations such as elementary exponential, logarithmic, root and
trigonometric functions.
All methods forward to java.lang.Math unless otherwise noted.
The package object
scala.sys
contains methods for reading
and altering core aspects of the virtual machine as well as the
world outside of it.
The package object
scala.sys
contains methods for reading
and altering core aspects of the virtual machine as well as the
world outside of it.
A tuple of 2 elements; the canonical representation of a scala.Product2 .
Element 1 of this Tuple2
Element 2 of this Tuple2
Create a new tuple with 2 elements.
Create a new tuple with 2 elements. Note that it is more idiomatic to create a Tuple2 via
(t1, t2)
Element 1 of this Tuple2
Element 2 of this Tuple2
Test two objects for inequality.
Test two objects for inequality.
true
if !(this == that), false otherwise.
Equivalent to
x.hashCode
except for boxed numeric types and
null
.
Equivalent to
x.hashCode
except for boxed numeric types and
null
.
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.
For
null
returns a hashcode where
null.hashCode
throws a
NullPointerException
.
a hash value consistent with ==
The expression
x == that
is equivalent to
if (x eq null) that eq null else x.equals(that)
.
The expression
x == that
is equivalent to
if (x eq null) that eq null else x.equals(that)
.
true
if the receiver object is equivalent to the argument;
false
otherwise.
A projection of element 1 of this Product.
A projection of element 2 of this Product.
Forces the compiler to treat the receiver object as having type
T0
,
even though doing so may violate type safety.
Forces the compiler to treat the receiver object as having type
T0
,
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 whether
asInstanceOf
has any effect at runtime.
It might do a runtime type test on the erasure of
T0
,
insert a conversion (such as boxing/unboxing), fill in a default value, or do nothing at all.
In particular,
asInstanceOf
is 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 where
asInstanceOf
is useful:
T0
automatically
Be careful of using
asInstanceOf
when
T0
is a primitive type.
When
T0
is primitive,
asInstanceOf
may insert a conversion instead of a type test.
If your intent is to convert, use a
toT
method (
x.toChar
,
x.toByte
, etc.).
the receiver object.
ClassCastException
if the receiver is not an instance of the erasure of
T0
,
if that can be checked on this platform
Create a copy of the receiver object.
Create a copy of the receiver object.
The default implementation of the
clone
method is platform dependent.
a copy of the receiver object.
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
).
The
eq
method implements an
equivalence relation
on
non-null instances of
AnyRef
, and has three additional properties:
x
and
y
of type
AnyRef
, multiple invocations of
x.eq(y)
consistently returns
true
or consistently returns
false
.
x
of type
AnyRef
,
x.eq(null)
and
null.eq(x)
returns
false
.
null.eq(null)
returns
true
.
When overriding the
equals
or
hashCode
methods, 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
).
true
if the argument is a reference to the receiver object;
false
otherwise.
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 the
finalize
method is invoked, as
well as the interaction between
finalize
and non-local returns
and exceptions, are all platform dependent.
not specified by SLS as a member of AnyRef
Returns the runtime class representation of the object.
Test whether the dynamic type of the receiver object has the same erasure as
T0
.
Test whether the dynamic type of the receiver object has the same erasure as
T0
.
Depending on what
T0
is, the test is done in one of the below ways:
T0
is a non-parameterized class type, e.g.
BigDecimal
: this method returns
true
if
the value of the receiver object is a
BigDecimal
or a subtype of
BigDecimal
.
T0
is a parameterized class type, e.g.
List[Int]
: this method returns
true
if
the value of the receiver object is some
List[X]
for any
X
.
For example,
List(1, 2, 3).isInstanceOf[List[String]]
will return true.
T0
is some singleton type
x.type
or literal
x
: this method returns
this.eq(x)
.
For example,
x.isInstanceOf[1]
is equivalent to
x.eq(1)
T0
is an intersection
X with Y
or
X & Y: this method is equivalent to
x.isInstanceOf[X] && x.isInstanceOf[Y]
T0
is a union
X | Y
: this method is equivalent to
x.isInstanceOf[X] || x.isInstanceOf[Y]
T0
is a type parameter or an abstract type member: this method is equivalent
to
isInstanceOf[U]
where
U
is
T0
's upper bound,
Any
if
T0
is unbounded.
For example,
x.isInstanceOf[A]
where
A
is an unbounded type parameter
will return true for any value of
x
.
This is exactly equivalent to the type pattern
_: T0
returns
true
if the receiver object is an instance of erasure of type
T0
;
false
otherwise.
Definition Classes
Any
Note
due to the unexpectedness of
List(1, 2, 3).isInstanceOf[List[String]]
returning true and
x.isInstanceOf[A]
where
A
is 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)
.
-
returns
-
true
if the argument is not a reference to the receiver object;
false
otherwise.
-
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
def
productArity
:
Int
The arity of this product.
def
productElement
(
n:
Int
)
:
Any
Returns the n-th projection of this product if 0 <= n < productArity,
otherwise throws an
IndexOutOfBoundsException
.
Returns the n-th projection of this product if 0 <= n < productArity,
otherwise throws an
IndexOutOfBoundsException
.
-
n
-
number of the projection to be returned
-
returns
-
same as
._(n+1)
, for example
productElement(0)
is the same as
._1
.
-
Definition Classes
-
Product2
→
Product
-
Annotations
-
@
throws
(
clazz =
classOf[IndexOutOfBoundsException]
)
-
Exceptions thrown
-
IndexOutOfBoundsException
if the
n
is out of range(n < 0 || n >= 2).
def
productElementNames
:
Iterator
[
String
]
An iterator over the names of all the elements of this product.
An iterator over the names of all the elements of this product.
-
Definition Classes
-
Product
def
swap
: (
T2
,
T1
)
Swaps the elements of this
Tuple
.
Swaps the elements of this
Tuple
.
-
returns
-
a new Tuple where the first element is the second element of this Tuple and the
second element is the first element of this Tuple.
final
def
synchronized
[
T0
]
(
arg0: =>
T0
)
:
T0
Executes the code in
body
with an exclusive lock on
this
.
Executes the code in
body
with an exclusive lock on
this
.
-
returns
-
the result of
body
-
Definition Classes
-
AnyRef
def
toString
()
:
String
Creates a String representation of this object.
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-
![]() |
有腹肌的李子 · 翟天临硕士论文抄袭学位被取消!你的论文抄了吗? 4 月前 |
This is the documentation for the Scala standard library.
Package structure
The scala package contains core types like
Int
,Float
,Array
orOption
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 frameworkscala.collection.immutable
- Immutable, sequential data-structures such asVector
,List
,Range
,HashMap
orHashSet
scala.collection.mutable
- Mutable, sequential data-structures such asArrayBuffer
,StringBuilder
,HashMap
orHashSet
scala.collection.concurrent
- Mutable, concurrent data-structures such asTrieMap
scala.concurrent
- Primitives for concurrent programming such asFutures
andPromises
scala.io
- Input and output operationsscala.math
- Basic math functions and additional numeric types likeBigInt
andBigDecimal
scala.sys
- Interaction with other processes and the operating systemscala.util.matching
- Regular expressionsOther 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 forjava.lang.String
.