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

Dart

Dart Problem Overview


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 ?

Dart Solutions


Solution 1 - Dart

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.

Solution 2 - Dart

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".

Solution 3 - Dart

No, wrappers are the only way.

Solution 4 - Dart

As dart is compiled into JavaScript, I tried something that works for JS, and guess what!? It worked for dart!

Basically, what you can do is put your value inside an object, and then any changes made on that field value inside that function will change the value outside that function as well.

Code (You can run this on dartpad.dev)

main() {
  var a = {"b": false};
  
  print("Before passing: " + a["b"].toString());
  trial(a);
  print("After passing: " + a["b"].toString());
}

trial(param) {
  param["b"] = true;
}

Output

Before passing: false
After passing: true

Solution 5 - Dart

You can use ValueNotifier

And, you can pass it as ValueListenable to classes or methods that needs to know up-to-date value, but should not edit it:

class Owner {
  final theValue = ValueNotifier(true);
  final user = User(theValue);
  ...
}

class User {
  final ValueListeneble<bool> theValue;
  
  User(this.theValue);

  ...
}


It provides more functionality than actually needed, but solves the problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEric LavoieView Question on Stackoverflow
Solution 1 - DartKai SellgrenView Answer on Stackoverflow
Solution 2 - DartMarioPView Answer on Stackoverflow
Solution 3 - DartJustin FagnaniView Answer on Stackoverflow
Solution 4 - DartArshdeep SinghView Answer on Stackoverflow
Solution 5 - Dartpolina-cView Answer on Stackoverflow