DataTx
DataTx

Reputation: 1869

How to Import Scala Map from Another Scala File

I'm new to scala and maybe I'm not searching using the right wording but I am basically trying to do the following. I'll write it in python to get the point across.

Say I have a python file called settings.py. And within the settings file I Have a dictionary called settings that looked like this: settings = {"env" : "prod", "token" : 1234}

I then have a main.py where I import the settings dictionary like so:

from settings import settings

How would I do this with a scala map that looked like:

val settings = Map("env" -> "prod", 
                   "token" -> 1234)

Upvotes: 0

Views: 197

Answers (1)

A top-level statement is not valid in Scala 2, except in the RELP (that will change in Scala 3).

The best way would be to wrap that in an object so you can import it.

// Settings.scala
package foo

object Settings {
  final val settings =
    Map(
      "env" -> "prod", 
      "token" -> 1234
    )
}

Which you can use like this:

// Main.scala
package foo

import Settings.settings

object Main extends App {
  println(settings)
}

Anyways, it seems you are pretty new to Scala and you do not understand basic concepts like packages and scopes. My advice would be to pick any tutorial or course, so you can learn all that.

Upvotes: 1

Related Questions