How can I protect classes, features, and implementation-level objects from the public Scala APIs?

I would like to reduce the level of implementation information from leaking into my public Scala APIs, and there are so many buttons for setting visibility that my head spins from trying to figure out:

What is the best way to protect implementation level classes, features, and objects from the public Scala APIs?

The best I can think of is to do:

package restaurant {
  trait Service {
    acceptOrder(dish: String, quantity: Int)
  }

  package impl {
    private class ServiceImpl {   // mark this & everything else in package as private
      acceptOrder(dish: String, quantity: Int) = println("order accepted")
    }

    private class ServiceHelper { // mark this & everything else in package as private
      def helpDoStuff() = ???
    }
  }
}

, impl 'private', ( ) . , Scala > < , , , . , , .

, ... , , .

: Scala. API. , , , .

IDEA: , , , IDEA . - . Intellij ( - : https://youtrack.jetbrains.com/issue/IDEA-144872)

+4
2

!

? , , . , , implames - , .

package restaurant

sealed trait Service {
  def acceptOrder(dish: String, quantity: Int): Unit
}

object Service {
  def create: Service = new Impl.ServiceImpl()

  private object Impl {
    class ServiceImpl extends Service {
      def acceptOrder(dish: String, quantity: Int) = println("order accepted")
    }

    class ServiceHelper {
      def helpDoStuff() = ???
    }
  }
}
+2

-

package restaurant {
  trait Service {
    acceptOrder(dish: String, quantity: Int)
  }

  private[restaurant] class ServiceImpl {
    acceptOrder(dish: String, quantity: Int) = println("order accepted")
  }

  private[restaurant] class ServiceHelper {
    def helpDoStuff() = ???
  }
}

, .

+1

Source: https://habr.com/ru/post/1606355/


All Articles