Reputation: 353
I have a set of scala class which I'm trying to utilize:
FileA.scala
abstract class ResourceType[T](val amount: T) {
}
class Cpu(amount: Double) extends ResourceType[Double](amount){
}
class Memory(amount: ByteSize) extends ResourceType[ByteSize](amount){
}
class Storage(amount: ByteSize) extends ResourceType[ByteSize](amount){
}
class RuntimeResources(cpus: Double = 1, memSize: ByteSize = 0.B, storageSize: ByteSize = 0.B) {
val cpu: Cpu = new Cpu(cpus)
val mem: Memory = new Memory(memSize)
val storage: Storage = new Storage(storageSize)
}
Then somewhere else in the codebase (different file, same package), FileB.scala
...
val x: Seq[RuntimeResources] = ...
...
x.map(_.mem.amount.toMB)
However, when I try to compile this I get
value amount is not a member of <package>.Memory
I think I am misunderstanding the access modifiers of the classes. I don't really understand why this doesn't compile. When I move the classes from FileA.scala
into FileB.scala
it seems to compile fine, however I can't move the code into that file for other reasons. Why does simply moving the code to from FileA.scala
to FileB.scala
allow it to compile, and is there any way around this?
Upvotes: 1
Views: 436
Reputation: 353
I found the reason why this would not compile.
I am working with a large codebase (100k+ LoC), and in another file in the same package there was a sealed trait Memory
which clashed with the Memory
class in the function above.
When I renamed one of the classes, it compiled fine. I am surprised the compiler does not flag that as an issue. It seemed that the trait took precedence in the compiler.
Edit: After discussion in the comments, I've filed a bug against scala for this issue: https://github.com/scala/bug/issues/12289
Upvotes: 1