#scala
In the following program we derive a class SeqBuffer
which allows us to store only sequences in the buffer by stating that type T
has to be a subtype of Seq[U]
for a new abstract type U
:
trait Buffer {
type T
val element: T
}
abstract class SeqBuffer extends Buffer {
type U
type T <: Seq[U]
def length = element.length
}
If you want to change selection, open document below and click on "Move attachment"
Abstract Types - Scala Documentations. Like value definitions, we can override type definitions in subclasses. This allows us to reveal more information about an abstract type by tightening the type bound (which describes possible concrete instantiations of the abstract type).
<span>In the following program we derive a class SeqBuffer which allows us to store only sequences in the buffer by stating that type T has to be a subtype of Seq[U] for a new abstract type U:
abstract class SeqBuffer extends Buffer {
type U
type T <: Seq[U]
def length = element.length
}
Traits or classes with abstract type members are often used in combination with anonymous class instantiations. To illustrate this, we now look at a program which deals with a sequence b Summary
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
Details