- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Strange case of object.type
Tue, 2010-12-28, 16:18
hi,
i am heaving some conceptual and aesthetic trouble with case objects used both as values and types. i am converting
some stuff in the form of class X( rate: Rate ) into class X[ R <: Rate ]() to strengthen compile-type type constraint checks.
my basic setup is:
sealed trait Rate
case object scalar extends Rate
case object control extends Rate
case object audio extends Rate
case object demand extends Rate
import collection.immutable.{IndexedSeq=>IIdxSeq}
trait GE[ +U ] {
def expand: IIdxSeq[ U ]
}
trait UGenIn[ R <: Rate ] extends GE[ UGenIn[ R ]] {
def expand: IIdxSeq[ UGenIn[ R ]] = IIdxSeq( this )
}
trait UGen
case class UGenOutProxy[ R <: Rate ]( source: UGen, outputIndex: Int, rate: R ) extends UGenIn[ R ]
now i am trying to define a concrete UGen.
first attempt (does not compile):
case class DiskInUGen(numChannels: Int, buf: GE[ UGenIn[ _ <: Rate ]], loop: GE[ UGenIn[ _ <: Rate ]])
extends UGen with GE[UGenIn[audio]] {
def inputs = IIdxSeq( buf, loop )
def expand = IIdxSeq.tabulate( inputs.size )( UGenOutProxy( this, _, audio ))
}
:37: error: not found: type audio
extends UGen with GE[UGenIn[audio]] {
^
i also tried GE[UGenIn[`audio`]], same error.
second attempt (does not compile):
case class DiskInUGen(numChannels: Int, buf: GE[ UGenIn[ _ <: Rate ]], loop: GE[ UGenIn[ _ <: Rate ]])
extends UGen with GE[UGenIn[audio.type]] {
def inputs = IIdxSeq( buf, loop )
def expand = IIdxSeq.tabulate( inputs.size )( UGenOutProxy( this, _, audio ))
}
:39: error: type mismatch;
found : UGenOutProxy[object audio]
required: UGenIn[audio.type]
def expand = IIdxSeq.tabulate( inputs.size )( UGenOutProxy( this, _, audio ))
^
third attempt (does compile):
case class DiskInUGen(numChannels: Int, buf: GE[ UGenIn[ _ <: Rate ]], loop: GE[ UGenIn[ _ <: Rate ]])
extends UGen with GE[UGenIn[audio.type]] {
def inputs = IIdxSeq( buf, loop )
def expand = IIdxSeq.tabulate( inputs.size )( UGenOutProxy[audio.type]( this, _, audio ))
}
so, i don't understand why audio is not a type in itself, but then (second attempt) the compiler puts it as UGenOutProxy[object audio]. so where does that audio.type come from, and why can't the compiler infer it in UGenOutProxy( this, _, audio ).
the object.type is really ugly and i would like to omit the .type if possible. since i want to use the rates for pattern matching and so forth, i also do not want to transform the case objects into some case classes (doesn't make sense anyway, since they have no arguments).
thanks for enlightenment,
-sciss-