Search code examples
scalacompanion-object

Access object inside of a companion object in scala


I have the following:

case class Location(name: String, level: Location.Level)

object Location {
  trait Level
  case object City extends Level
  case object State extends Level
}

If I try and access City (from another source file), I get an error saying something like

found   : model.Location.City.type
required: model.Level

I can think of some work-arounds, but I'm wondering if there's a way to keep my names the same i.e. I'd like to access City by typing Location.City.

EDIT:

I'm accessing it like this:

import the.package.name._
Location.City

Solution

  • Your error message says it all: you are not asking for a Location.Level but model.Level, where model must be either one of your packages or a value and you have a path-dependent type there. So you must have another type Level in your code.