Outdated Notice
Scala lets you supply default values for constructor parameters. For example, in previous lessons we showed that you can define a Socket
class like this:
class Socket(var timeout: Int, var linger: Int) {
override def toString = s"timeout: $timeout, linger: $linger"
}
That’s nice, but you can make this class better by supplying default values for the timeout
and linger
parameters:
class Socket(var timeout: Int = 2000, var linger: Int = 3000) {
override def toString = s"timeout: $timeout, linger: $linger"
}
By supplying default values for the parameters, you can now create a new Socket
in a variety of different ways:
new Socket()
new Socket(1000)
new Socket(4000, 6000)
Here’s what those examples look like in the REPL:
scala> new Socket()
res0: Socket = timeout: 2000, linger: 3000
scala> new Socket(1000)
res1: Socket = timeout: 1000, linger: 3000
scala> new Socket(4000, 6000)
res2: Socket = timeout: 4000, linger: 6000
Benefits
Supplying default constructor parameters has at least two benefits:
- You provide preferred, default values for your parameters
- You let consumers of your class override those values for their own needs
As shown in the examples, a third benefit is that it lets consumers construct new Socket
instances in at least three different ways, as if it had three class constructors.
Bonus: Named parameters
Another nice thing about Scala is that you can use named parameters when creating a new instance of a class. For instance, given this class:
class Socket(var timeout: Int, var linger: Int) {
override def toString = s"timeout: $timeout, linger: $linger"
}
you can create a new Socket
like this:
val s = new Socket(timeout=2000, linger=3000)
This feature comes in handy from time to time, such as when all of the class constructor parameters have the same type, such as the Int
parameters in this example. For example, some people find that this code:
val s = new Socket(timeout=2000, linger=3000)
is more readable than this code:
val s = new Socket(2000, 3000)
Contributors to this page:
Contents
- Introduction
- Prelude꞉ A Taste of Scala
- Preliminaries
- Scala Features
- Hello, World
- Hello, World - Version 2
- The Scala REPL
- Two Types of Variables
- The Type is Optional
- A Few Built-In Types
- Two Notes About Strings
- Command-Line I/O
- Control Structures
- The if/then/else Construct
- for Loops
- for Expressions
- match Expressions
- try/catch/finally Expressions
- Scala Classes
- Auxiliary Class Constructors
- Supplying Default Values for Constructor Parameters
- A First Look at Scala Methods
- Enumerations (and a Complete Pizza Class)
- Scala Traits and Abstract Classes
- Using Scala Traits as Interfaces
- Using Scala Traits Like Abstract Classes
- Abstract Classes
- Scala Collections
- The ArrayBuffer Class
- The List Class
- The Vector Class
- The Map Class
- The Set Class
- Anonymous Functions
- Common Sequence Methods
- Common Map Methods
- A Few Miscellaneous Items
- Tuples
- An OOP Example
- sbt and ScalaTest
- The most used scala build tool (sbt)
- Using ScalaTest with sbt
- Writing BDD Style Tests with ScalaTest and sbt
- Functional Programming
- Pure Functions
- Passing Functions Around
- No Null Values
- Companion Objects
- Case Classes
- Case Objects
- Functional Error Handling in Scala
- Concurrency
- Scala Futures
- Where To Go Next