Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Generally Scala is immutable, you usually have to handle mutable cases yourself. Also case classes are immutable by nature and their copy method is generated by the compiler. So yes, they would share the same object reference. This is one of the reasons immutability is nice.</p> <p>Your second question is a bit different. In that case the classes are constructed one after the other in seperate contexts.</p> <p>It's also a good idea to check what's being compiled:</p> <pre><code>&gt;scalac -print test.scala [[syntax trees at end of cleanup]] // test.scala package test { class Foo extends Object { private[this] val list: List = _; &lt;stable&gt; &lt;accessor&gt; def list(): List = Foo.this.list; def &lt;init&gt;(): b.Foo = { Foo.super.&lt;init&gt;(); Foo.this.list = immutable.this.List.apply(scala.this.Predef.wrapIntArray(Array[Int]{1, 2, 3, 4, 5, 6})); () } } } </code></pre> <p>From this we can see Scala creating a new list each time. Changing this to <code>var</code> won't change anything, we can check:</p> <pre><code>&gt;scalac -print test.scala [[syntax trees at end of cleanup]] // test.scala package test { class Foo extends Object { private[this] var list: List = _; &lt;accessor&gt; def list(): List = Foo.this.list; &lt;accessor&gt; def list_=(x$1: List): Unit = Foo.this.list = x$1; def &lt;init&gt;(): b.Foo = { Foo.super.&lt;init&gt;(); Foo.this.list = immutable.this.List.apply(scala.this.Predef.wrapIntArray(Array[Int]{1, 2, 3, 4, 5, 6})); () } } } </code></pre> <p>It only generated a setter method for it (<code>def list_=(x$1: List)</code>).</p> <p>If you would like to reference the same list in both cases then initialize it with an object's default list:</p> <pre><code>object Foo { val DefaultList = List(1,2,3,4,5,6) } class Foo { var list = Foo.DefaultList } </code></pre> <p>Which compiles to the following:</p> <pre><code>&gt;scalac -print test.scala [[syntax trees at end of cleanup]] // test.scala package test { object Foo extends Object { private[this] val DefaultList: List = _; &lt;stable&gt; &lt;accessor&gt; def DefaultList(): List = Foo.this.DefaultList; def &lt;init&gt;(): test.Foo.type = { Foo.super.&lt;init&gt;(); Foo.this.DefaultList = immutable.this.List.apply(scala.this.Predef.wrapIntArray(Array[Int]{1, 2, 3, 4, 5, 6})); () } }; class Foo extends Object { private[this] var list: List = _; &lt;accessor&gt; def list(): List = Foo.this.list; &lt;accessor&gt; def list_=(x$1: List): Unit = Foo.this.list = x$1; def &lt;init&gt;(): test.Foo = { Foo.super.&lt;init&gt;(); Foo.this.list = Foo.DefaultList(); () } } } </code></pre> <p>As you can see the list is only created once and then the reference through the <code>def DefaultList(): List</code> assigned on each Foo class instantiation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload