![](https://csdnimg.cn/release/download_crawler_static/11184414/bg14.jpg)
class were missing, it would be straightforward for any Scala programmer to write an implementation,
for instance, by wrapping Java's classjava.math.BigInteger (in fact that's how Scala's BigInt class is
implemented).
Of course, you could also use Java's class directly. But the result is not nearly as pleasant, because
although Java allows you to create new types, those types don't feel much like native language support:
import java.math.BigInteger
def factorial(x: BigInteger): BigInteger =
if (x == BigInteger.ZERO)
BigInteger.ONE
else
x.multiply(factorial(x.subtract(BigInteger.ONE)))
BigInt is representative of many other number-like types—big decimals, complex numbers, rational
numbers, confidence intervals, polynomials—the list goes on. Some programming languages
implement some of these types natively. For instance, Lisp, Haskell, and Pythonimplement big
integers; Fortran and Python implement complex numbers. But any language that attempted to
implement all of these abstractions at the same time would simply become too big to be manageable.
What's more, even if such a language were to exist, some applications would surely benefit from other
number-like types that were not supplied. So the approach of attempting to provide everything in one
language doesn't scale very well. Instead, Scala allows users to grow and adapt the language in the
directions they need by defining easy-to-use libraries that feel like native language support.
Growing new control constructs
The previous example demonstrates that Scala lets you add new types that can be used as conveniently
as built-in types. The same extension principle also applies to control structures. This kind of
extensibility is illustrated by Akka, a Scala API for "actor-based" concurrent programming.
As multicore processors continue to proliferate in the coming years, achieving acceptable performance
may increasingly require that you exploit more parallelism in your applications. Often, this will mean
rewriting your code so that computations are distributed over several concurrent threads. Unfortunately,
creating dependable multi-threaded applications has proven challenging in practice. Java's threading
model is built around shared memory and locking, a model that is often difficult to reason about,
especially as systems scale up in size and complexity. It is hard to be sure you don't have a race
condition or deadlock lurking—something that didn't show up during testing, but might just show up in
production. An arguably safer alternative is a message passing architecture, such as the "actors"
approach used by the Erlang programming language.
Java comes with a rich, thread-based concurrency library. Scala programs can use it like any other Java
API. However, Akka is an additional Scala library that implements an actor model similar to Erlang's.
Actors are concurrency abstractions that can be implemented on top of threads. They communicate by
sending messages to each other. An actor can perform two basic operations, message send and receive.