15

I may be having a silly problem here... I can't seem to figure out how to make a constructor without parameters in Scala. I know I can just write the whole thing in the class body (especially because it's the only constructor I need), but it doesn't quite feel right.

What I have:

    class Foo {
        //some init code

        //...
    }

What I'd like (but doesn't work as it wants me to call another constructor first):

    class Foo {
        // The only constructor
        def this() = { 
            //some init code
        }

        //...
    }

4 Answers 4

20

All classes in Scala have a primary constructor and optionally some auxiliary constructors (which must defer to the primary constructor or another auxiliary constructor).

The issue in your case is that in both cases you've defined the primary constructor as taking no arguments - and then in the second case you try to define an auxiliary constructor with the same signature. This doesn't work, for the same reason that the following wouldn't compile:

// Primary constructor takes a String
class Foo(s: String) {
    // Auxiliary constructor also takes a String?? (compile error)
    def this(a: String) {
        this(a)
    }
}

This isn't anything to do with the fact that the constructor is no-args; the following compiles for example:

class Foo(s: String) {
    // alternative no-arg constructor (with different signature from primary)
    def this() {
        this("Default value from auxiliary constructor")
    }
}

In particular, in your second example, your comment "the only constructor" is wrong. Auxiliary constructors are always secondary to the primary constructor, and cannot ever be the only constructor.

FWIW, the first example is the only option open to you, but it looks fine to me. If you've just started using Scala I'm sure it will start to feel right soon enough - and it's important to eschew Java-esque ways of doing things when there are more idiomatic alternatives.

Sign up to request clarification or add additional context in comments.

4 Comments

Auxiliary constructors can also defer to other auxiliary constructors defined before.
@jean-phillipe: Thanks, you're right. I just realised that wasn't accurate in the broader scope myself; post corrected now.
Thanks, something bothered me about Scala constructors from the start, but it never got in my way too much... it's all clear now.
There is also the possibility of making the primary constructor private. The syntax is class Foo private ().
14

For what it's worth you can introduce an extra scope to "mark" the init code.

class Foo {
   {
      // init code here
   }
}

2 Comments

I very much like this. Coming from Java, having the default constructor code in the class body just feels too messy!
Thx for the nice words, but if I were you I wouldn't get used to it. I've been in the Scala landscape for the last 7 years and haven't seen this pattern being used anywhere even once. ;)
5

Well putting the init code in the class body is the only way to have a constructor without parameters. I suppose if you want you could do something like :

class Foo {

  private def init {
    //init code here
  }

  init()
}

that's as close as you're gonna get.

Comments

5

The init code is the body of the method. But you can do this, if it bothers you all that much:

class Foo {
        locally { 
            //some init code
        }
}

2 Comments

What is locally in this context? Is it a reserved word? Can it be referenced anywhere?
@user It is an inlined method defined on Predef, which returns the value of the block. It can be referenced anywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.