Scala case class with cyclic referencing and value updationIs Java “pass-by-reference” or “pass-by-value”?Is the Scala 2.8 collections library a case of “the longest suicide note in history”?Difference between object and class in ScalaWhat is the difference between Scala's case class and class?How to clone a case class instance and change just one field in Scala?Why do val and def implement abstract methods at different times?scala overriding class parametersInvoking reflected case class constructor in Scala, Part 2Scala Multiple Nested Case ClasesWhy is the each iteration parameter val and not var in for loop in scalaIs it possible to declare a val before assignment/initialization in Scala?
Factorio analysis: data munging
How to determine if window is maximised or minimised from bash script
What Brexit solution does the DUP want?
Divisibility of sum of multinomials
Is there a familial term for apples and pears?
Why does the small signal analysis work? (Intuition)
How to make payment on the internet without leaving a money trail?
Does the radius of the Spirit Guardians spell depend on the size of the caster?
Email Account under attack (really) - anything I can do?
Should I join an office cleaning event for free?
Circuitry of TV splitters
Why do we use polarized capacitor?
Weird behaviour when using querySelector
What typically incentivizes a professor to change jobs to a lower ranking university?
Can town administrative "code" overule state laws like those forbidding trespassing?
Copenhagen passport control - US citizen
Shell script can be run only with sh command
Example of a relative pronoun
"The augmented fourth (A4) and the diminished fifth (d5) are the only augmented and diminished intervals that appear in diatonic scales"
Where to refill my bottle in India?
XeLaTeX and pdfLaTeX ignore hyphenation
DOS, create pipe for stdin/stdout of command.com(or 4dos.com) in C or Batch?
How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?
Why is an old chain unsafe?
Scala case class with cyclic referencing and value updation
Is Java “pass-by-reference” or “pass-by-value”?Is the Scala 2.8 collections library a case of “the longest suicide note in history”?Difference between object and class in ScalaWhat is the difference between Scala's case class and class?How to clone a case class instance and change just one field in Scala?Why do val and def implement abstract methods at different times?scala overriding class parametersInvoking reflected case class constructor in Scala, Part 2Scala Multiple Nested Case ClasesWhy is the each iteration parameter val and not var in for loop in scalaIs it possible to declare a val before assignment/initialization in Scala?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Im trying to understand the following code, how the value in the variables a
of case class dummy is being updated in the following code.
In the value
method, I'm assigning the value of a
to b
, and adding new Node to b
, which inturn it is reflecting the changes in a
also. I don't understand how the value is a
is being changed without even re-assignement.
object test
def main(args: Array[String])
case class abc(str: String, var ele: abc)
case class dummy(a: abc)
def value() =
var b = a
println(s"Value of B before changing : $b.str") // Value of B before changing : Good
println(s"Value of A before changing : $a.str") // Value of A before changing : Good
val newObj = abc(" Morning", null)
b.ele = newObj
println(s"Value of A After changing : $a.str,$a.ele.str") // Value of A After changing : Good, Morning
println(s"Value of B after changing : $b.str,$b.ele.str") // Value of B after changing : Good, Morning
val testObj = dummy(abc("Good", null))
testObj.value()
scala
add a comment |
Im trying to understand the following code, how the value in the variables a
of case class dummy is being updated in the following code.
In the value
method, I'm assigning the value of a
to b
, and adding new Node to b
, which inturn it is reflecting the changes in a
also. I don't understand how the value is a
is being changed without even re-assignement.
object test
def main(args: Array[String])
case class abc(str: String, var ele: abc)
case class dummy(a: abc)
def value() =
var b = a
println(s"Value of B before changing : $b.str") // Value of B before changing : Good
println(s"Value of A before changing : $a.str") // Value of A before changing : Good
val newObj = abc(" Morning", null)
b.ele = newObj
println(s"Value of A After changing : $a.str,$a.ele.str") // Value of A After changing : Good, Morning
println(s"Value of B after changing : $b.str,$b.ele.str") // Value of B after changing : Good, Morning
val testObj = dummy(abc("Good", null))
testObj.value()
scala
add a comment |
Im trying to understand the following code, how the value in the variables a
of case class dummy is being updated in the following code.
In the value
method, I'm assigning the value of a
to b
, and adding new Node to b
, which inturn it is reflecting the changes in a
also. I don't understand how the value is a
is being changed without even re-assignement.
object test
def main(args: Array[String])
case class abc(str: String, var ele: abc)
case class dummy(a: abc)
def value() =
var b = a
println(s"Value of B before changing : $b.str") // Value of B before changing : Good
println(s"Value of A before changing : $a.str") // Value of A before changing : Good
val newObj = abc(" Morning", null)
b.ele = newObj
println(s"Value of A After changing : $a.str,$a.ele.str") // Value of A After changing : Good, Morning
println(s"Value of B after changing : $b.str,$b.ele.str") // Value of B after changing : Good, Morning
val testObj = dummy(abc("Good", null))
testObj.value()
scala
Im trying to understand the following code, how the value in the variables a
of case class dummy is being updated in the following code.
In the value
method, I'm assigning the value of a
to b
, and adding new Node to b
, which inturn it is reflecting the changes in a
also. I don't understand how the value is a
is being changed without even re-assignement.
object test
def main(args: Array[String])
case class abc(str: String, var ele: abc)
case class dummy(a: abc)
def value() =
var b = a
println(s"Value of B before changing : $b.str") // Value of B before changing : Good
println(s"Value of A before changing : $a.str") // Value of A before changing : Good
val newObj = abc(" Morning", null)
b.ele = newObj
println(s"Value of A After changing : $a.str,$a.ele.str") // Value of A After changing : Good, Morning
println(s"Value of B after changing : $b.str,$b.ele.str") // Value of B after changing : Good, Morning
val testObj = dummy(abc("Good", null))
testObj.value()
scala
scala
edited Mar 9 at 4:20
Er. Dinesh
172114
172114
asked Mar 9 at 4:10
Guda uma shankerGuda uma shanker
659
659
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
var b = a
This does not make a copy.
You now have both variables pointing to the same object, and any change made to the state of b
will also reflect in a
.
For this (and other reasons) you are highly encouraged to avoid mutable state. If your case class did not have a var
in it, it would be immutable and you can be sure that no matter who you give your a
to, it will still be intact when you get it back.
(The second bad habit to shake is using null
. No need for that in Scala, make the type an Option
instead).
as you have said ,var b = a
'doesn't make a copy' is this statement valid only for case class with contains mutable fields.
– Guda uma shanker
Mar 9 at 4:31
1
It is the same for all objects (including arrays). A=
just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).
– Thilo
Mar 9 at 4:37
I tried the same for this codevar a = 2; var b = a; b = 3; println(a,b) // (2,3)
, according to statement it should update the value in a also, but it is not doing .
– Guda uma shanker
Mar 9 at 4:41
1
b = 3
does not update the object thatb
is pointing to. It setsb
to another value. That does not affect other variables (likea
) that happen to also contain the old value. Your original code hadb.ele = newObj
(NOTb = newObj
). That keptb
pointing at the same object (whicha
was also looking at), but updatedele
inside of that object (and thus, alsoa.ele
which at that point was just another name/variable/label for the same thing).
– Thilo
Mar 9 at 5:04
Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.
– Thilo
Mar 9 at 5:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073906%2fscala-case-class-with-cyclic-referencing-and-value-updation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
var b = a
This does not make a copy.
You now have both variables pointing to the same object, and any change made to the state of b
will also reflect in a
.
For this (and other reasons) you are highly encouraged to avoid mutable state. If your case class did not have a var
in it, it would be immutable and you can be sure that no matter who you give your a
to, it will still be intact when you get it back.
(The second bad habit to shake is using null
. No need for that in Scala, make the type an Option
instead).
as you have said ,var b = a
'doesn't make a copy' is this statement valid only for case class with contains mutable fields.
– Guda uma shanker
Mar 9 at 4:31
1
It is the same for all objects (including arrays). A=
just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).
– Thilo
Mar 9 at 4:37
I tried the same for this codevar a = 2; var b = a; b = 3; println(a,b) // (2,3)
, according to statement it should update the value in a also, but it is not doing .
– Guda uma shanker
Mar 9 at 4:41
1
b = 3
does not update the object thatb
is pointing to. It setsb
to another value. That does not affect other variables (likea
) that happen to also contain the old value. Your original code hadb.ele = newObj
(NOTb = newObj
). That keptb
pointing at the same object (whicha
was also looking at), but updatedele
inside of that object (and thus, alsoa.ele
which at that point was just another name/variable/label for the same thing).
– Thilo
Mar 9 at 5:04
Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.
– Thilo
Mar 9 at 5:05
add a comment |
var b = a
This does not make a copy.
You now have both variables pointing to the same object, and any change made to the state of b
will also reflect in a
.
For this (and other reasons) you are highly encouraged to avoid mutable state. If your case class did not have a var
in it, it would be immutable and you can be sure that no matter who you give your a
to, it will still be intact when you get it back.
(The second bad habit to shake is using null
. No need for that in Scala, make the type an Option
instead).
as you have said ,var b = a
'doesn't make a copy' is this statement valid only for case class with contains mutable fields.
– Guda uma shanker
Mar 9 at 4:31
1
It is the same for all objects (including arrays). A=
just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).
– Thilo
Mar 9 at 4:37
I tried the same for this codevar a = 2; var b = a; b = 3; println(a,b) // (2,3)
, according to statement it should update the value in a also, but it is not doing .
– Guda uma shanker
Mar 9 at 4:41
1
b = 3
does not update the object thatb
is pointing to. It setsb
to another value. That does not affect other variables (likea
) that happen to also contain the old value. Your original code hadb.ele = newObj
(NOTb = newObj
). That keptb
pointing at the same object (whicha
was also looking at), but updatedele
inside of that object (and thus, alsoa.ele
which at that point was just another name/variable/label for the same thing).
– Thilo
Mar 9 at 5:04
Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.
– Thilo
Mar 9 at 5:05
add a comment |
var b = a
This does not make a copy.
You now have both variables pointing to the same object, and any change made to the state of b
will also reflect in a
.
For this (and other reasons) you are highly encouraged to avoid mutable state. If your case class did not have a var
in it, it would be immutable and you can be sure that no matter who you give your a
to, it will still be intact when you get it back.
(The second bad habit to shake is using null
. No need for that in Scala, make the type an Option
instead).
var b = a
This does not make a copy.
You now have both variables pointing to the same object, and any change made to the state of b
will also reflect in a
.
For this (and other reasons) you are highly encouraged to avoid mutable state. If your case class did not have a var
in it, it would be immutable and you can be sure that no matter who you give your a
to, it will still be intact when you get it back.
(The second bad habit to shake is using null
. No need for that in Scala, make the type an Option
instead).
answered Mar 9 at 4:19
ThiloThilo
197k79423577
197k79423577
as you have said ,var b = a
'doesn't make a copy' is this statement valid only for case class with contains mutable fields.
– Guda uma shanker
Mar 9 at 4:31
1
It is the same for all objects (including arrays). A=
just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).
– Thilo
Mar 9 at 4:37
I tried the same for this codevar a = 2; var b = a; b = 3; println(a,b) // (2,3)
, according to statement it should update the value in a also, but it is not doing .
– Guda uma shanker
Mar 9 at 4:41
1
b = 3
does not update the object thatb
is pointing to. It setsb
to another value. That does not affect other variables (likea
) that happen to also contain the old value. Your original code hadb.ele = newObj
(NOTb = newObj
). That keptb
pointing at the same object (whicha
was also looking at), but updatedele
inside of that object (and thus, alsoa.ele
which at that point was just another name/variable/label for the same thing).
– Thilo
Mar 9 at 5:04
Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.
– Thilo
Mar 9 at 5:05
add a comment |
as you have said ,var b = a
'doesn't make a copy' is this statement valid only for case class with contains mutable fields.
– Guda uma shanker
Mar 9 at 4:31
1
It is the same for all objects (including arrays). A=
just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).
– Thilo
Mar 9 at 4:37
I tried the same for this codevar a = 2; var b = a; b = 3; println(a,b) // (2,3)
, according to statement it should update the value in a also, but it is not doing .
– Guda uma shanker
Mar 9 at 4:41
1
b = 3
does not update the object thatb
is pointing to. It setsb
to another value. That does not affect other variables (likea
) that happen to also contain the old value. Your original code hadb.ele = newObj
(NOTb = newObj
). That keptb
pointing at the same object (whicha
was also looking at), but updatedele
inside of that object (and thus, alsoa.ele
which at that point was just another name/variable/label for the same thing).
– Thilo
Mar 9 at 5:04
Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.
– Thilo
Mar 9 at 5:05
as you have said ,
var b = a
'doesn't make a copy' is this statement valid only for case class with contains mutable fields.– Guda uma shanker
Mar 9 at 4:31
as you have said ,
var b = a
'doesn't make a copy' is this statement valid only for case class with contains mutable fields.– Guda uma shanker
Mar 9 at 4:31
1
1
It is the same for all objects (including arrays). A
=
just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).– Thilo
Mar 9 at 4:37
It is the same for all objects (including arrays). A
=
just assigns pointers to objects to another variable (which will then refer to the same object, not to a copy).– Thilo
Mar 9 at 4:37
I tried the same for this code
var a = 2; var b = a; b = 3; println(a,b) // (2,3)
, according to statement it should update the value in a also, but it is not doing .– Guda uma shanker
Mar 9 at 4:41
I tried the same for this code
var a = 2; var b = a; b = 3; println(a,b) // (2,3)
, according to statement it should update the value in a also, but it is not doing .– Guda uma shanker
Mar 9 at 4:41
1
1
b = 3
does not update the object that b
is pointing to. It sets b
to another value. That does not affect other variables (like a
) that happen to also contain the old value. Your original code had b.ele = newObj
(NOT b = newObj
). That kept b
pointing at the same object (which a
was also looking at), but updated ele
inside of that object (and thus, also a.ele
which at that point was just another name/variable/label for the same thing).– Thilo
Mar 9 at 5:04
b = 3
does not update the object that b
is pointing to. It sets b
to another value. That does not affect other variables (like a
) that happen to also contain the old value. Your original code had b.ele = newObj
(NOT b = newObj
). That kept b
pointing at the same object (which a
was also looking at), but updated ele
inside of that object (and thus, also a.ele
which at that point was just another name/variable/label for the same thing).– Thilo
Mar 9 at 5:04
Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.
– Thilo
Mar 9 at 5:05
Read this (even though it is about Java, the same applies to Scala): stackoverflow.com/questions/40480/… It is important to understand that an object and a variable pointing to the object are not the same thing.
– Thilo
Mar 9 at 5:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073906%2fscala-case-class-with-cyclic-referencing-and-value-updation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown