Scala values initialization - scala

Why in this example, no error is thrown and b ends up holding default value?
scala> val b = a; val a = 5
b: Int = 0
a: Int = 5

When you do this in the REPL, you are effectively doing:
class Foobar { val b = a; val a = 5 }
b and a are assigned to in order, so at the time when you're assigning b, there is a field a, but it hasn't yet been assigned to, so it has the default value of 0. In Java, you can't do this because you can't reference a field before it is defined. I believe you can do this in Scala to allow lazy initialisation.
You can see this more clearly if you use the following code:
scala> class Foobar {
println("a=" + a)
val b = a
println("a=" + a)
val a = 5
println("a=" + a)
}
defined class Foobar
scala> new Foobar().b
a=0
a=0
a=5
res6: Int = 0
You can have the correct values assigned if you make a a method:
class Foobar { val b = a; def a = 5 }
defined class Foobar
scala> new Foobar().b
res2: Int = 5
or you can make b a lazy val:
scala> class Foobar { lazy val b = a; val a = 5 }
defined class Foobar
scala> new Foobar().b
res5: Int = 5

Related

Can I define the = operator for my class in Scala

I know that one can define an operator in Scala like this :
class value(var valu:Int) {
def +(i:Int) = { this.valu + i }
def ==>(i:Int ) = { this.valu = i }
}
But I cannot seem to overload the = operator like this :
class value(var valu:Int) {
def =(i:Int) = { this.valu = i }
}
Do you know if there is any way to do this?
The syntax for making mutable objects isn't obvious and isn't encountered often because mutability is generally undesirable.
class Value(private var valu:Int) {
def update(i:Int) :Unit = valu = i
}
val v = new Value(19)
v() = 52
= is a reserved word like yield, so to use it as an identifier, you put it in backticks, though I suspect no one does that:
scala> class C(var i: Int) { def `=`(n: Int) = i = n }
defined class C
scala> val c = new C(42)
c: C = C#9efcd90
scala> c.`=`(27)
scala> c.i
res1: Int = 27
scala> c `=` 5
scala> c.i
res3: Int = 5
Compare:
scala> val yield = 2
^
error: illegal start of simple pattern
scala> val `yield` = 2
yield: Int = 2

How to access inner class elements in scala

I have a simple inner class variable, how do i access it in scala?
class Outer {
class Inner {
var x = 1
}}
object Main {
def main(args: Array[String]): Unit = {
val o = new Outer
val i = new o.Inner
println(i.x)
}
}
The problem is that IntelliJ complains that it cannot resolve x, but when i run the program it works fine.
you can simply use .member_name to access variables in scala.
scala> class Outer {
class Inner {
var x = 1 //it can be val which is immutable
}}
defined class Outer
scala> val o = new Outer
o: Outer = Outer#358b0b42
scala> val i = new o.Inner
i: o.Inner = Outer$Inner#512f2c7d
scala> i.x
res13: Int = 1
since your example has x defined as mutable, you can change the value of x,
scala> i.x = 100
i.x: Int = 100
scala> i.x
res14: Int = 100
See working example - https://scastie.scala-lang.org/prayagupd/C9k9an4ASdaISnohbYQBmA
If you don't really need Outer to be a class, you can define it as singleton,
scala> object Outer {
| class Inner {
| var x = 1 //it can be val which is immutable
| }}
defined object Outer
then, simple instantiate Inner and access variables,
scala> val inner = new Outer.Inner
inner: Outer.Inner = Outer$Inner#4bcdd11
scala> inner.x
res2: Int = 1
Regarding not working on intellij, File | Invalidate Caches/Restart... should work

Unexpected Result when Overriding 'val'

In Scala 2.10.4, Given the following class:
scala> class Foo {
| val x = true
| val f = if (x) 100 else 200
| }
defined class Foo
The following two examples make sense to me:
scala> new Foo {}.f
res0: Int = 100
scala> new Foo { override val x = false}.f
res1: Int = 200
But, why doesn't this call return 100?
scala> new Foo { override val x = true }.f
res2: Int = 200
Because vals aren't initialized more than once, x is actually null (or false for a default Boolean) during the initialization of Foo, and then initialized in the anonymous class that is extending Foo in your example.
We can test it more easily with an AnyRef:
class Foo {
val x = ""
val f = if (x == null) "x is null" else "not null"
}
scala> new Foo { override val x = "a" }.f
res10: String = x is null
scala> new Foo {}.f
res11: String = not null
There's a full explanation in the Scala FAQ. Excerpt:
Naturally when a val is overridden, it is not initialized more than once. So though x2 in the above example is seemingly defined at every point, this is not the case: an overridden val will appear to be null during the construction of superclasses, as will an abstract val.
A simple way to avoid this would be to use a lazy val or def, if the val being referenced may be overridden.
Additionally, you can use the -Xcheckinit compiler flag to warn you about potential initialization errors like this.

Why must forward referenced values inside blocks in Scala be lazy?

The scope of a name introduced by a declaration or definition is the
whole statement sequence containing the binding. However, there is a
restriction on forward references in blocks: In a statement sequence
s[1]...s[n] making up a block, if a simple name in s[i] refers to
an entity defined by s[j] where j >= i, then for all s[k]
between and including s[i] and s[j],
s[k] cannot be a variable definition.
If s[k] is a value definition, it must be lazy.
Edit: I am not sure Mikaël Mayer's answer actually explained everything. Consider:
object Test {
def main(args: Array[String]) {
println(x)
lazy val x: Int = 6
}
}
Here, the lazy value x definitely has to be read/evaluated before it is actually defined in the code! Which would contradict Mikaël's claim that lazy evaluation does away with the need to evaluate things before they are defined.
Normally you cannot have this:
val e: Int = 2
val a: Int = b+c
val b: Int = c
val c: Int = 1
val d: Int = 0
because value c is not yet defined at the time of the definition of a. Because a references c, all values between a and c should be lazy so that the dependency is avoided
val e: Int = 2
lazy val a: Int = b+c
lazy val b: Int = c
lazy val c: Int = 1
val d: Int = 0
This in fact translates a, b and c as objects whose value is initialized when it is read, which would be after the declaration, i.e. this would be equivalent to:
val e: Int = 2
var a: LazyEval[Int] = null
var b: LazyEval[Int] = null
var c: LazyEval[Int] = null
a = new LazyEval[Int] {
def evalInternal() = b.eval() + c.eval()
}
b = new LazyEval[Int] {
def evalInternal() = c.eval()
}
c = new LazyEval[Int] {
def evalInternal() = 1
}
val d = 0
where LazyEval would be something like the following (implemented by the compiler itself)
class LazyEval[T] {
var value: T = _
var computed: Boolean = false
def evalInternal(): T // Abstract method to be overriden
def eval(): T = {
if(computed) value else {
value = evalInternal()
computed = true
value
}
}
}
Edit
vals don't really exist in java. They are local variables or do not exist in computation. Therefore, the declaration of lazy val exists before anything is done. And remember that closures are implemented in Scala.
Your block would be rewritten as it:
object Test {
def main(args: Array[String]) {
// Declare all variables, val, vars.
var x: Lazy[Int] = null
// No more variables to declare. Lazy/or not variable definitions
x = new LazyEval[Int] {
def evalInternal() = 6
}
// Now the code starts
println(x)
}
}
You're trying to avoid references to entities which are provably uninitialized (or which are maybe uninitialized).
In a block, assignments occur in source order, but in a class template, members can be overridden and initialized early.
For instance,
{ val a = b ; val b = 1 } // if allowed, value of a is undefined
but in a template
class X { val a = b ; val b = 1 } // warning only
val x = new { override val b = 2 } with X
x.a // this is 2
class Y(override val b: Int) extends X // similarly
You also want to avoid this:
locally {
def a = c
val b = 2 // everything in-between must be lazy, too
def c = b + 1
}
Local objects are explicitly the same as lazy vals:
{ object p { val x = o.y } ; object o { val y = 1 } }
Other kinds of forward reference:
{ val x: X = 3 ; type X = Int }
The spec talks about forward references to "entities" -- a "name refers to an entity" -- which elsewhere means both terms and types, but obviously it really means only terms here.
It will let you harm yourself:
{ def a: Int = b ; def b: Int = a; a }
Maybe your mode of self-destruction must be well-defined. Then it's OK.

Scala Named Arguments

I'm looking at this Named Arguments example in Scala in Depth:
scala> class Parent {
| def foo(bar: Int = 1, baz: Int = 2): Int = bar + baz
| }
defined class Parent
scala> class Child extends Parent {
| override def foo(baz: Int = 3, bar: Int = 4): Int = super.foo(baz, bar)
| }
defined class Child
scala> val p = new Parent
p: Parent = Parent#6100756c
scala> p.foo()
res1: Int = 3
scala> val x = new Child
x: Child = Child#70605759
Calling x.foo() evaluates to 7 since Child#foo has default arguments of 3 and 4.
scala> x.foo()
res3: Int = 7
Instantiate a new Child at run-time, but Parent at compile-time. This may or may not be correct
scala> val y: Parent = new Child
y: Parent = Child#540b6fd1
Calling x.foo() evaluates to 7 since Child#foo has default arguments of 3 and 4.
scala> y.foo()
res5: Int = 7
Calling x.foo() evaluates to 4 since Child#foo has a default baz argument of 3.
scala> x.foo(bar = 1)
res6: Int = 4
However, I don't understand why y.foo(bar = 1) returns 5. I would've expected Child#foo to be evaluated since y is a Child type. Passing in a bar of 1 to foo means that baz's default is 3. And so it should produce 4. But my understanding is of course incorrect.
scala> y.foo(bar = 1)
res7: Int = 5
There are 2 reasons:
Default parameters implementation
scala compiler creates helper methods for default parameters:
val p = new Parent()
val c = new Child()
p.`foo$default$1`
// Int = 1
p.`foo$default$2`
// Int = 2
c.`foo$default$1`
// Int = 3
c.`foo$default$2`
// Int = 4
This is why you could use not only constants, but also fields and methods for default parameters:
def test(i: Int = util.Random.nextInt) = i
test()
// Int = -1102682999
test()
// Int = -1994652923
Named parameters implementation
There are no named parameters after compilation - all parameters are positional.
So since bar is second parameter of Child#foo this code:
c.foo(bar = 1)
// Int = 4
is translated by compiler to this:
c.foo(c.`foo$default$1`, /*bar = */1)
// Int = 4
But since bar is first parameter of Parent#foo this code:
val tmp: Parent = c
tmp.foo(bar = 1)
// Int = 5
is translated to this:
tmp.foo(/*bar = */1, tmp.`foo$default$2`)
// Int = 5
As we already know c.foo$default$2 returns 4, so c.foo(1, 4) returns 5.