#compound-types #scala
what the type of the parameter obj
is. If it’s Cloneable
then the object can be clone
d, but not reset
; if it’s Resetable
we can reset
it, but there is no clone
operation. To avoid type casts in such a situation, we can specify the type of obj
to be both Cloneable
and Resetable
. This compound type is written like this in Scala: Cloneable with Resetable
.
If you want to change selection, open document below and click on "Move attachment"
Compound Types - Scala Documentation Unit
}
Now suppose we want to write a function cloneAndReset which takes an object, clones it and resets the original object:
def cloneAndReset(obj: ?): Cloneable = {
val cloned = obj.clone()
obj.reset
cloned
}
The question arises <span>what the type of the parameter obj is. If it’s Cloneable then the object can be cloned, but not reset; if it’s Resetable we can reset it, but there is no clone operation. To avoid type casts in such a situation, we can specify the type of obj to be both Cloneable and Resetable. This compound type is written like this in Scala: Cloneable with Resetable.
Here’s the updated function:
def cloneAndReset(obj: Cloneable with Resetable): Cloneable = {
//...
}
Compound types can consist of several object types and they may have a single re Summary
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
Details