Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a singleton object in Scala with runtime params

Tags:

scala

I'm trying to create a singleton object with parameters which are specified by runtime. Example:

object NetworkPusher {
  val networkAdress = ???
  ...
 }

Imagine the networkAdress param comes from the command-line. How can I make a workaround to do this?

like image 724
λ Allquantor λ Avatar asked Jan 06 '15 14:01

λ Allquantor λ


1 Answers

Singletons are initialized lazily.

scala> :pa
// Entering paste mode (ctrl-D to finish)

object Net {
  val address = Config.address
}
object Config { var address = 0L }

// Exiting paste mode, now interpreting.

defined object Net
defined object Config

scala> Config.address = "1234".toLong
Config.address: Long = 1234

scala> Net.address
res0: Long = 1234

FWIW.

like image 75
som-snytt Avatar answered Sep 19 '22 21:09

som-snytt