Reputation: 63022
For the following HwLogger.PersisterType enumeration:
object HwLogger extends Serializable {
object PersisterType extends Enumeration {
type Persisters = Value
val FilePersister, HdfsPersister, SocketPersister = Value
}
}
In the companion class - how do we access it? Notice the slew of imports below ..
class HwLogger extends Serializable {
import collection.mutable.ArrayBuffer
import HwLogger.PersisterType._
import HwLogger.PersisterType
var persisterTypes = ArrayBuffer[PersisterType]() // Compiler says " error: not found: type PersisterType"
def setDefaults : Unit = {
import PersisterType._
persisterTypes ++ FilePersister ++ HdfsPersister
}
}
UPDATE After the accepted answer (and syntax correction on the arraybuffer append operators) here is the corrected code:
object HwLogger extends Serializable {
object PersisterType extends Enumeration {
type Persisters = Value
val FilePersister, HdfsPersister, SocketPersister = Value
}
}
class HwLogger extends Serializable {
import collection.mutable.ArrayBuffer
import HwLogger.PersisterType._
import HwLogger.PersisterType
var persisterTypes = ArrayBuffer[Persisters]() // This is the fix (Persisters instead of PersisterType)
def setDefaults : Unit = {
import PersisterType._
persisterTypes :+ FilePersister :+ HdfsPersister
}
}
** Another update **
Changing the enumeration object will avoid the hassles:
object HwLogger extends Serializable {
object PersisterType extends Enumeration {
type PersisterType = Value // Changed the "type" to be same as object name
val FilePersister, HdfsPersister, SocketPersister = Value
}
}
class HwLogger extends Serializable {
import collection.mutable.ArrayBuffer
import HwLogger.PersisterType._
import HwLogger.PersisterType
var persisterTypes = ArrayBuffer[PersisterType]() // Revert back to PersisterType
def setDefaults : Unit = {
import PersisterType._
persisterTypes :+ FilePersister :+ HdfsPersister
}
}
Upvotes: 2
Views: 36
Reputation: 13667
The type of elements in the Enumeration is Persisters
, not PersisterType
. PersisterType
is only a value. You do need to import the types and values inside of PersisterType
to use the Enumeration
.
class HwLogger extends Serializable {
import collection.mutable.ArrayBuffer
import HwLogger.PersisterType._
val persisterTypes = ArrayBuffer[Persisters]()
def setDefaults: Unit = {
persisterTypes += FilePersister += HdfsPersister
}
}
Upvotes: 3