- Home /
Your "own" structs ... are they indeed passed by value?
I'd like to check something with a Unityscript expert .... thanks!
Generally in Javascript - as I understand it - if something is a struct it is passed by value. (A new copy is made - if you change it you are doing nothing to the original). Whereas, if something is a class, it's passed by reference. (No copy is made of the data. You are actually changing the values in the original.) [See footnote 1]
My question .. as an everyday matter in UnityScript, you make "structs" or pseudo-structs, like this ...
class EnormousThing extends System.ValueType
{
// huge amounts of stuff here
}
In fact, is it correct that such "pseudo-structs" are ALSO passed by value?
Another way to express this:
If you use "class EnormousThing" it will be passed by reference ("just as with all classes in Javascript"), whereas, if you use the "pseudo-struct" idiom: "class EnormousThing extends System.ValueType" it will be passed by value (|just as with real structs in Javascript")
is this exactly correct?
{As a further bonus question, in fact, is the only difference in adding "extends System.ValueType" in fact the value-versus-reference behaviour so described? Is that the only reason to add "extends System.ValueType" ..? Is there really any other difference in using that qualifier?}
[Footnote 1] Although - this is a detail - cleverly the reference itself is I believe just a copy, so, if you set it to null you have done nothing to the original. Please completely ignore this issue in the question at hand.
Answer by IGoByChad · Jun 02, 2012 at 02:18 PM
It is correct that it will be passed by value. Another difference is that you will no longer be able to use a default constructor to initialize your values. You will get the default on all your members. (This link might help: http://answers.unity3d.com/questions/23044/struct-in-javascript.html
"It is correct that it will be passed by value."
Fantastic. Thanks for the confirmation!
Your answer
Follow this Question
Related Questions
UnityScript - Class reference 1 Answer
Raycast script help? 1 Answer
Namespace declaration for UnityScript or Unity Javascript 0 Answers
#define directive in UnityScript 1 Answer
Is it possible to pass a variable by reference in javascript 1 Answer