0

Any way to resolve class in object in Scala. I want to use an instance of Configuration class in Configuration object.

package application

import com.google.inject.Singleton
import play.api.Environment


@Singleton
class Configuration(env: Environment) {
    private lazy val config = play.api.Configuration.load(env)

    val venturesTime = config.getBoolean("ventures.time")
}

object Configuration {

}
2
  • 1. by declaring òbject Configuration you're already making a singleton object. 2. Having previously mentioned singleton object and a class with same name in the same source file, is called a companion object. For example, collection classes take advantage of this and one only needs to use val x = List(...), which utilizes apply method the singleton object. So, your question is not quite clear. Perhaps these notes will help you clarify it. Commented Mar 18, 2016 at 4:29
  • Just to add to what @kaskelotti said. You can always do a new Configuration() inside the companion object. Commented Mar 18, 2016 at 4:34

1 Answer 1

2

You cannot - in this scenario.

If you get an instance of your configuration via dependency injection, then that's only available inside the class. While the class can access the companion object's methods, the other way around doesn't work, because which instance should it take?

If you need the configuration inside the companion object, you should pass it to the respective method as a parameter.

On a different note, rather than injecting the environment, you could as well inject the configuration directly.

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

Comments

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.