initialise a var in scala - scala

I have a class where I like to initialize my var by reading a configfile, which produces intermediate objects/vals, which I would like to group and hide in a method.
Here is the bare minimum of the problem - I call the ctor with a param i, in reality a File to parse, and the init-method generates the String s, in reality more complicated than here, with a lot of intermediate objects being created:
class Foo (val i: Int) {
var s : String;
def init () {
s = "" + i
}
init ()
}
This will produce the error: class Foo needs to be abstract, since variable s is not defined. In this example it is easy to solve by setting the String to "": var s = "";, but in reality the object is more complex than String, without an apropriate Null-implementation.
I know, I can use an Option, which works for more complicated things than String too:
var s : Option [String] = None
def init () {
s = Some ("" + i)
}
or I can dispense with my methodcall. Using an Option will force me to write Some over and over again, without much benefit, since there is no need for a None else than to initialize it that way I thought I could.
Is there another way to achieve my goal?

var s : Whatever = _ will initialize s to the default value for Whatever (null for reference types, 0 for numbers, false for bools etc.)

Instead of creating separate methods for initialization, you should perform the initialization using the following way :
class Foo(val i: Int) {
var s: String = {
var s0 = " "
s0 += i
// do some more stuff with s0
s0
}
var dashedDate = {
val dashed = new SimpleDateFormat("yy-MM-dd")
dashed.format(updated)
}
// Initializing more than one field:
var (x, y, z) = {
var x0, y0, z0 = 0
// some calculations
(x0, y0, z0)
}
}

Honestly, why are you using var? Why not just do:
val rootObject = readFile(filename)
This would make the most sense to me.

Related

Scala: Initialization of var in class

I am beginner of Scala, I try to make a class and use of it, which class has variables (var) and some processing (in def). I write class like as follows;
class Example() {
var ExampleVar0 = 0
var ExampleVar1 = 0
def ExampleProcessing(_arg0: Int, _arg1: Int) : (Int, Int) = {
ExampleVar0 = _arg0 + 1
ExampleVar1 = _arg1 - 1
(ExampleVar0*2, ExampleVar1/2)
}
}
I want to initialize ExampleVar* and to keep and update the value. I use the class in other class;
var result0
var result1
val exp = Example()
exp.ExampleProcessing(5, 6)(result0, result1)
The both of result0 and result1 is zero, seems always write with zero by statement of the var ExampleVar0 = 0. I checked the value of the var (println(exp.ExampleVar0.toString)) then it is always zero. I might misunderstand about initialization of var.
Could you please point out where is my misunderstand and how to solve it?
You appear to be using result0 and result1 as method parameters, which does not look right. Try this:
val exp = Example()
val (result0, result1) = exp.ExampleProcessing(5, 6)
In this case the var does not seem to be relevant or necessary, but I will give the usual warning that it is best to avoid var and use immutable data structures.

How can I change mutable incremental number to be functional

I have this code UniqueKeyGenerator, there is a mutable variable number to be incrementally added, because I want to make the generated value to be predictable, I wonder how can I change it to be value instead of variable
object UniqueKeyGenerator {
var number = 0
def generate(suiteClass: Class[_]): String = {
number = number + 1
suiteClass.getCanonicalName + "." + this.number
}
}
Many thanks in advance
If you're just trying to express this with a val instead of a var, you could use an Iterator.
object UniqueKeyGenerator {
val numbers = Iterator.iterate(0)(_ + 1)
def generate(suiteClass: Class[_]): String =
s"${suiteClass.getCanonicalName}.${numbers.next()}"
}
Otherwise, I'm not sure what you're asking - maybe provide some more context?
If you have all of the inputs up front, then you can write something like this:
Seq(classOf[String], classOf[Int], classOf[String]).zipWithIndex
.map({ case (suiteClass, i) => s"${suiteClass.getCanonicalName}.$i" })
// res: List(java.lang.String.0, int.1, java.lang.String.2)
Aside from an Iterator you can also use a closure, for example if the id you want to generate is not just the next natural number, but a result of a more complex expression.
val nextId = {
var id = 0
() => {
id = id + 1 // change it to something more complex if you like
id
}
}
The "id" here is still a variable, but it is not accessible from the outside. The only way you can use it is by calling nextId:
def generate(suiteClass: Class[_]): String = {
suiteClass.getCanonicalName + "." + nextId()
}

How to mimic Scala's Map/Array assignment syntax in my own class

Following is a simple map entry assignment:
scala> var myl = mutable.Map[String,String]()
myl: scala.collection.mutable.Map[String,String] = Map()
myl("abc") = "123"
I would like to mimic that assignment structure in my own class that works with mutable Tuple's. Now, "getting" a value from a Map is achieved via the "apply" method:
e.g mutable.HashMap:
override def apply(key: A): B = {
val result = findEntry(key)
if (result eq null) default(key)
else result.value
}
I was not however able to find how the map entry is "set" via myMap("myKey") = "myval". A pointer to the Scala source code to do that would be appreciated. Thanks.
The method you want to implement is called update() and takes two parameters, one for the input value passed in parentheses and the other for the assigned value.
class QueryParams {
var params = ""
def update(name: String, value: String) { params += s"$name=$value&" }
}
For example:
val p = new QueryParams()
p("q") = "SFO"
p("start") = "10"
p("rows") = "10"
p.params

how do I increment an integer variable I passed into a function in Scala?

I declared a variable outside the function like this:
var s: Int = 0
passed it such as this:
def function(s: Int): Boolean={
s += 1
return true
}
but the error lines wont go away under the "s +=" for the life of me. I tried everything. I am new to Scala btw.
First of all, I will repeat my words of caution: solution below is both obscure and inefficient, if it possible try to stick with values.
implicit class MutableInt(var value: Int) {
def inc() = { value+=1 }
}
def function(s: MutableInt): Boolean={
s.inc() // parentheses here to denote that method has side effects
return true
}
And here is code in action:
scala> val x: MutableInt = 0
x: MutableInt = MutableInt#44e70ff
scala> function(x)
res0: Boolean = true
scala> x.value
res1: Int = 1
If you just want continuously increasing integers, you can use a Stream.
val numberStream = Stream.iterate(0)(_ + 1).iterator
That creates an iterator over a never-ending stream of number, starting at zero. Then, to get the next number, call
val number: Int = numberStream.next
I have also just started using Scala this was my work around.
var s: Int = 0
def function(s: Int): Boolean={
var newS = s
newS = newS + 1
s = newS
return true
}
From What i read you are not passing the same "s" into your function as is in the rest of the code. I am sure there is a even better way but this is working for me.
You don't.
A var is a name that refers to a reference which might be changed. When you call a function, you pass the reference itself, and a new name gets bound to it.
So, to change what reference the name points to, you need a reference to whatever contains the name. If it is an object, that's easy enough. If it is a local variable, then it is not possible.
See also call by reference, though I don't think this question is a true duplicate.
If you just want to increment a variable starting with 3
val nextId = { var i = 3; () => { i += 1; i } }
then invoke it:
nextId()

Can I copy a closure in Scala?

This is perhaps a half-baked idea, but can I copy the environment of an expression? Consider the following example:
class foo[T](block: => T) {
var data = { () => block }
def run() = { data() }
}
var a = 5
var b = new foo({ a += 1; println(a) })
var c = new foo({ a += 1; println(a) })
b.run()
c.run()
b.run()
c.run()
I get the following output:
6
7
8
9
But what I really want is the following:
6
6
7
7
In short, I want foo to have copies of the variables in the block instead of referring to them, once the initial value has been resolved. Can this be done? scala.util.DynamicVariable seems promising, but I'm having a hard time understanding how I might use it in the example above.
If you want to make a copy, then why not make a copy?
var c = new foo({ var aa = a + 1; println(aa) })
If you want to take an existing closure that refers to a variable, and turn it into a closure that refers to a copy of that variable, then I'm afraid it's not possible.
In any case, mutable variables should not be used except in very rare circumstances and in a very controlled manner. That is, not juggling their references.
I think your problem is that you're closing over a (and thus changing its value as your program runs), rather than closing over a different variable initialized from a. You might want to try something more like this:
class bar[T](block: (T) => T, a: T) {
var cur = a
def run() = { cur = block(cur); cur }
}
var a = 5
var b = new bar((r: Int) => { val q = r + 1; println(q); q }, a)
var c = new bar((r: Int) => { val q = r + 1; println(q); q }, a)
b.run()
c.run()
b.run()
c.run()
Output:
6
6
7
7
However, note that unlike your code, this will not affect the value of the a variable as it runs.
1) As n.m. already mentioned, you should prefer immutable concepts when using closures.
2) On the other hand this is exactly what closures are for. They don't capture the value of the variabe, they capture the variable itself! Your question is kinda upside-down. You actually are capturing the environment - the environment of your variable. If you don't need the same variable than copy it as n.m. or Ian McLaird respectively suggested.