3

I want to implement init method kind of functionality in my object. for ex , my object structure is as follows:

object MyObject {
  var set: Set[String] = _
  var map : Map[String, String] =_
  var list: List[Integer] =_ 
  init()  

  def init() {
    // read external file and initialze variables
    set = ..
    map = ..
    list = .. 
  }
}

I have defined a init method and called init method from object itself. Is this a correct way to do it or can i achieve the same functionality through any better way?

2 Answers 2

4

You definitely don't want to have those things as vars, but the good news is you don't have to. How about something like this:

object MyObject {

  private[this] val data: List[String] = {
    // pretend we are reading this from a file
    List("1","2","3")
  }

  val set: Set[String] = data.toSet
  val map: Map[String, String] = data.map(x => (x,x)).toMap
  val list: List[Int] = data.map(_.toInt)
}

If you like having the init() function, you can do something like this (though it's a bit clunkier):

object MyObject {

  private[this] def init() = {
    val data: List[String] = {
      // pretend we are reading this from a file
      List("1","2","3")
    }

    val set = data.toSet
    val map = data.map(x => (x,x)).toMap
    val list = data.map(_.toInt)

    (set, map, list)        
  }

  val (
    set: Set[String],
    map: Map[String, String],
    list: List[Int]) 
      = init()
}
Sign up to request clarification or add additional context in comments.

4 Comments

In your second example, you could drop the type declaration from the tuple unpacking for val (set, map, list), but perhaps that would make the field a bit ambiguous as for what it's type actually is
@Yuval, True. (And they're not required in the first example either.) I was just trying to keep the syntax as close to the original as possible.
In the first example ,all the variables set, map and list have been initialized from the List "data" . This might not be my case . My question was more of a use case , where we have to read a xml file and create mutiple datastructure out of it. For ex: i might have multiple maps as well for different use case. But i guess, your 2nd example would fit all the scenarios
@Alok, In the first case, the declaration body of the object is basically just a function so you can do anything you want in it; a nested init() function is entirely superfluous.
2

General explanation

In Scala the body of the object is the init-Method.

So this is completely equivalent to the version you described above.

object MyObject {
  private[this] val data: List[String] = {List("1","2","3")}

  var mySet: Set[String] = data.toSet
  var myMap: Map[String, String] = data.map(x => (x,x)).toMap
  var list: List[Int] = data.map(_.toInt)
}

and please do not redefine map this will lead to a lot of pain.

Example with external resource:

conf file conf.xml

<config>
  <size>42</size>
</config>

scala object Example.scala

object Example {
  var size : Int =  (XML.loadFile("conf.xml") \\ "size").text.toInt

  println("I am initialized on object creation: >" + size + "<")
}

usage in REPL

scala> :load Example.scala
Loading Example.scala...

import scala.xml.XML
defined object Example

scala> Example
I am initialized on object creation: >42<
res0: Example.type = Example$@3c73951

2 Comments

This wouldnt serve my purpose . I need a method , to initialize all the variables, since i need to parse a xml file to initialize these var, which would be possible in a method only. Also there could be multiple map which i might have to initialize
I will extend the example to show you how you can do this.

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.