113

I've been curious about the impact of not having an explicit primary constructor in Scala, just the contents of the class body.

In particular, I suspect that the private or protected constructor pattern, that is, controlling construction through the companion object or another class or object's methods might not have an obvious implementation.

Am I wrong? If so, how is it done?

3
  • You could have a Scala singleton (with the object keyword, that is), and define your class as private within that singleton, and have methods of the singleton for constructing your objects.
    – Paggas
    Nov 13, 2009 at 17:06
  • @Paggas, unfortunately when you return an instance of a class marked private out of it's scope it won't compile, even when returned from a method of it's in scope companion object. Nov 13, 2009 at 17:36
  • This is done quite profusely throughout the Scalaz source code. The concept is also known as an abstract algebraic data type. Oct 28, 2011 at 7:49

2 Answers 2

197

You can declare the default constructor as private/protected by inserting the appropriate keyword between the class name and the parameter list, like this:

class Foo private () { 
  /* class body goes here... */
}
6
  • Thanks Aleksander, Can you please tell me whether this is presented in one of the scala books or in the language specification? Sorry I can't upvote yet. Nov 13, 2009 at 17:31
  • I just glanced over "Programming Scala"'s explanation of constructors (pages 92-95) and I don't see it mentioned there. I actually found the answer to your question in an old changelog, but I've never seen it mentioned anywhere else before. Link: scala-lang.org/node/43#2.4.0 Nov 13, 2009 at 17:36
  • 19
    Pag 414 of "Programming in Scala". Page 97 of Wampler's Programming Scala. Page 60 of Subramaniam's Programming Scala. I don't have a PDF of Beginning Scala with me right now to check it out. Nov 13, 2009 at 18:53
  • Oh, I see it now on page 97. Thanks. Nov 13, 2009 at 19:21
  • 1
    Thanks both for the further research, I've got the Wampler book, but only on my phone and clearly haven't read it thoroughly but I've found that it complements the Odersky book surprisingly well. Nov 13, 2009 at 20:53
66

Aleksander's answer is correct, but Programming in Scala offers an additional alternative:

sealed trait Foo {
 // interface
}

object Foo {
  def apply(...): Foo = // public constructor

  private class FooImpl(...) extends Foo { ... } // real class
}
17
  • 18
    Popping in years later to say: I think this is a good answer to the question but a bad solution to the problem. If some future programmer were to Aleksander's code, he would say "Ah, the primary constructor is private but other constructors are not." If that programmer were to look at Daniel's code, he would say, "Ah, they are using a Factory pattern to compensate for Scala's inability to mark the default constructors as private. Wait, Scala's can mark the default constructors as private! What is going on here?!?" In other words, a bad WTF/LOC ratio. Jul 23, 2011 at 17:09
  • 20
    @Malvolio I do not quite agree. This pattern not only make the primary constructor private, but also the implementation, forcing the user to use the interface (trait). That has its own value. As for someone thinking something because he/she doesn't know the language -- piffle! To quote Kenny Tilton, learn the damn language! Jul 25, 2011 at 2:42
  • 7
    It should be mentioned somewhere that this approach means not using the new keyword. Mar 14, 2013 at 20:16
  • 1
    One caveat with this approach is that someone could still instantiate a Foo through their own implementation. That could be seen as an advantage or disadvantage depending on the reason for controlling construction.
    – aij
    Dec 19, 2013 at 1:15
  • 1
    @aij True, so I just made it so that can't happen anymore. :) Dec 19, 2013 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.