Search code examples
scalaplayframework-2.0slickscala-2.10play-slick

Scala, restrict visibility on a package to a single object


I'm beginning with Slick (and Scala) on playframework. My project is structured with .scala files who contains all classes and objects for a domain concept.

package models

class Folder(val id: Option[Long], var name: String, val childrens:Set)

object Folder{
  val folders = new db.Folders

  def get(id: Long)(implicit s: Session): Option[Folder] = {
    Query(users).where(_.id === id).firstOption.map { r: db.Row =>
      new Folder(r.id, r.name, Set.empty]) // TODO load childrens for folder
    }
  }
}

//  Attempt to segregate and maybe restrict the persistence code
package db {

  case class Row(id: Option[Long], name: String)

  class Folders extends Table[Row]("folders") {

    def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
    def name = column[String]("name", O.NotNull)
    def * = id.? ~ name <> (map _, unmap _)
    // ...   
  }
}

Is there some Scala constructs who can restrict the inner package visibility to the Folder object only ?

Thanks


Solution

  • Some options:

    1. You could declare db as a private object inside of the Folder companion object,
    2. You could declare db as a trait, make the members protected and mix that into the Folder companion object
    3. You could put all of the Folder stuff in one package and make the stuff you want hidden package private (the syntax would be private[folder-package]).