score:7
Accepted answer
in scala there are two idiomatic ways how to achieve that.
- constructor private to the class and companion object.
factory has access to constructor, while anyone else doesn't:
class foo private[foo](val x: int)
object foo {
def apply(x:int) = new foo(x)
}
val foo = new foo(1) // cannot compile
val foo1 = foo(1) //compiles fine
- sealed abstract class.
in scala sealed
class can be extended only in the same source file it is defined.
i suggest to make foo
sealed abstract class
and return anonymous child of foo
in object's apply method.
sealed abstract class foo(val x:int)
object foo {
def apply(x:int):foo = new foo(x) {}
}
in this case foo
can be created nowhere except the file where it is defined.
upd: actually, this question was already discussed on stackoverflow.
upd2: added brief overview of both methods.
Source: stackoverflow.com
Related Query
- Restrict Construction of Scala Class
- Restrict class constructor visibility in Scala 2.9
- Restrict class to trait and structural subtype in Scala
- Scala - Conditionally add traits to class instance during construction
- Scala restrict class parameter value
- Difference between object and class in Scala
- IntelliJ Scala Plugin's case class indentation is absurd
- Unable to create Scala class on IntelliJ
- How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration
- Scala case class inheritance
- Class broken error with Joda Time using Scala
- Case class to map in Scala
- Extending scala case class without constantly duplicating constructors vals?
- What is the difference between a class and a type in Scala (and Java)?
- How to convert Row of a Scala DataFrame into case class most efficiently?
- How to get around the Scala case class limit of 22 fields?
- How to reload a class or package in Scala REPL?
- scala class constructor parameters
- Scala class and case class == comparison
- Meaning of additional colon in Scala class parametrization
- Generating a class from string and instantiating it in Scala 2.10
- Can a Scala class extend multiple classes?
- Scala case class private constructor but public apply method
- Could not find or load main class in scala in intellij IDE
- Scala case class extending Product with Serializable
- How to add a new Class in a Scala Compiler Plugin?
- Scala Macros: Making a Map out of fields of a class in Scala
- Scala client composition with Traits vs implementing an abstract class
- Scala class to implement two Java Interfaces - how?
- Get companion object of class by given generic type Scala
More Query from same tag
- Build Actions for all classes that mixin a trait
- Process-level parallelism in Scala
- Play Slick 2.1.0 This DBMS allows only a single AutoInc column to be returned from an INSERT
- Does Scalatest have any support for assumptions?
- How to solve this Scala/Play compilation error (wrong type being returned)?
- scala.MatchError in Swing table rendererComponent
- Scala play controller function type
- How to support marshalling and unmarshalling in Akka HTTP with JSON Spray for HashMap fields?
- ConcurrentModificationException when using Spark collectionAccumulator
- SBT: Simplest way to separate plugins.sbt
- Lazy arguments in lambda
- Scala Set: + vs. ++
- Spark Streaming stateful transformation mapWithState function getting error java.util.NoSuchElementException: None.get
- Scala: How to treat an Any like an Array or Seq?
- Convert a Map[String, String] to Map[String, Int] in Scala
- Spark/Scala 1.6 how to use dataframe groupby agg to implement following logical?
- Scala compiler doesn't recognize tail recursion
- How to assign an array to a row of 2d array in Scala
- Not a member of Product with Serializable
- settings.maxPrintString for Scala 2.9 REPL
- In these cases, the Scala value class will be "boxed", right?
- How to change log level in spark?
- Play WS exception: javax.net.ssl.SSLException: Received fatal alert: internal_error
- Using Java models with Scala. (value findById is not a member of object)
- Scala generic method. Accept any sequence of nummerical values
- How to get the properties values those that are defined in the shell (bash) script file using Scala 2.11 code?
- `getOrElse` to stop process or return different class type
- ScalaJS: How to convert String to String?
- Scala and encapsulation?
- Querying on Date in Mongo