Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to pass a primitive parameter by reference in Dart?

Tags:

dart

I would like to pass a primitive (int, bool, ...) by reference. I found a discussion about it (paragraph "Passing value types by reference") here: value types in Dart, but I still wonder if there is a way to do it in Dart (except using an object wrapper) ? Any development ?

like image 486
Eric Lavoie Avatar asked Aug 15 '13 17:08

Eric Lavoie


People also ask

Are primitive types passed by value or reference?

Primitive types get passed by value, object references get passed by value. Java doesn't pass objects. It passes object references - so if anyone asks how does Java pass objects, the answer is: "it doesn't".

Does Dart pass by reference or value?

Dart does not support passing by reference. Dart is only passed by value, just like Java. Java also does not support reference passing.

How do you pass variable by reference in flutter?

You cannot pass a variable by reference. There needs to be separate assignments to _email and _password in your code, because each assignment can only assign to one of them. You can then fiddle with how to choose which assignment to use, but there needs to be two separate assignments somewhere.


2 Answers

The Dart language does not support this and I doubt it ever will, but the future will tell.

Primitives will be passed by value, and as already mentioned here, the only way to 'pass primitives by reference' is by wrapping them like:

class PrimitiveWrapper {   var value;   PrimitiveWrapper(this.value); }  void alter(PrimitiveWrapper data) {   data.value++; }  main() {   var data = new PrimitiveWrapper(5);   print(data.value); // 5   alter(data);   print(data.value); // 6 } 

If you don't want to do that, then you need to find another way around your problem.

One case where I see people needing to pass by reference is that they have some sort of value they want to pass to functions in a class:

class Foo {   void doFoo() {     var i = 0;     ...     doBar(i); // We want to alter i in doBar().     ...     i++;   }    void doBar(i) {     i++;   } } 

In this case you could just make i a class member instead.

like image 146
Kai Sellgren Avatar answered Sep 21 '22 19:09

Kai Sellgren


They are passed by reference. It just doesn't matter because the "primitive" types don't have methods to change their internal value.

Correct me if I'm wrong, but maybe you are misunderstanding what "passing by reference" means? I'm assuming you want to do something like param1 = 10 and want this value to still be 10 when you return from your method. But references aren't pointers. When you assign the parameter a new value (with = operator), this change won't be reflected in the calling method. This is still true with non-primitive types (classes).

Example:

class Test {   int val;   Test(this.val); }  void main() {   Test t = new Test(1);   fn1(t);   print(t.val); // 2   fn2(t);   print(t.val); // still 2, because "t" has been assigned a new instance in fn2() }  void fn1(Test t) {   print(t.val); // 1   t.val = 2; }  void fn2(Test t) {   t = new Test(10);   print(t.val); // 10 } 

EDIT I tried to make my answer more clear, based on the comments, but somehow I can't seem to phrase it right without causing more confusion. Basically, when someone coming from Java says "parameters are passed by reference", they mean what a C/C++ developer would mean by saying "parameters are passed as pointers".

like image 45
MarioP Avatar answered Sep 17 '22 19:09

MarioP