Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass by reference or pointer or value in Dart

Tags:

flutter

dart

I am trying to write some codes that a variable can be added up by other class's function as testing for some works. I do not want to write return, extends, global variable but still can add the value. Any Ideas? Or is it impossible?

void main() {
  bbb B = new bbb();
  B.bOut();
  
}
class bbb{
  int b = 1;
  void bOut(){
    aaa A = aaa(this.b);
    A.aAdd();
    print(b);
  }
}
class aaa{
  int variableA;
  aaa(this.variableA);
  void aAdd (){
    variableA++;
  }
}

The output is 1

like image 349
Wesley Yam Avatar asked Aug 03 '19 13:08

Wesley Yam


People also ask

Is DART pass by value or reference?

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

Is flutter pass by reference?

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.

Does Dart have pointer?

In Dart, as in many other object oriented programming languages, objects are always pass-by-value. But you can still pass pointers to objects and arrays by value.


Video Answer


3 Answers

Dart passes-by-value where the value is a reference to the object

As jamesdlin said:

I would consider Dart to be like many object-oriented languages in that it is always pass-by-value and that the value of an object is a reference to it. In a true pass-by-reference system, a callee can modify variables in the caller (as with C++'s reference parameters).

What does that mean?

Say I write my home address on a piece of paper and give it to you. If you go away and tear up the paper or cross out the address and write a different one, that doesn't change my home or my address. I still have the same home and the same address no matter what you do to the piece of paper I gave you.

However, if you use the address that I wrote on the paper to find my home, you could paint the building or take away the trash or plant a flower. That would change some things about my home, but it wouldn't change the address.

It's the same thing with Dart. You can pass a variable off as an argument to some function. If the object that the variable refers to is mutable, the function can change the state of the object, but it can't change where that object is stored in memory.

Example

Here is the Dart version of the story above, inspired from this Java example.

void main() {
  Home addressToMyHome = Home('blue');
  Home myAddressCopy = addressToMyHome;

  print('My home is ${addressToMyHome.color}.');
  
  print('Here is my address.');
  writeNewAddressOnPaper(addressToMyHome);
  print('But my home is still ${addressToMyHome.color}.');

  print('Here is my address again.');
  goToMyHomeAndPaintIt(addressToMyHome);
  print('Now my home is ${addressToMyHome.color}.');
  
  if (addressToMyHome == myAddressCopy) {
    print('My address has not changed, though.');
  }
}

void writeNewAddressOnPaper(Home home) {
  home = Home('purple');
  print('You made a new ${home.color} home.');
}

void goToMyHomeAndPaintIt(Home home) {
  home.color = 'purple';
  print('You are painting my home ${home.color}.');
}

class Home {
  String color;
  Home(this.color);
}

This prints the following story:

My home is blue.
Here is my address.
You made a new purple home.
But my home is still blue.
Here is my address again.
You are painting my home purple.
Now my home is purple.
My address has not changed, though.

Note that the variable addressToMyHome does not contain the Home object itself, but only a value of where the Home object is stored in memory.

like image 158
Suragch Avatar answered Nov 15 '22 09:11

Suragch


Welcome to SO Wesley!

In Dart, as in many other object oriented programming languages, objects are always pass-by-value. But you can still pass pointers to objects and arrays by value. Thanks to @jamesdlin for pointing that out in the comments. For an more indepth look on this subject: Is Java pass-by-reference or pass-by-value?

Instead of passing this.b you could simply pass a reference to the object: bbb and access the variable b. I've prepared a simple example:

Output:

0
1

Code:

class Wrapper {
  int i;
}

test() {
  Wrapper wrapper = Wrapper();

  wrapper.i = 0;
  print(wrapper.i);

  increase(wrapper);
  print(wrapper.i);
}

increase(Wrapper wrapper) {
  wrapper.i++;
}
like image 30
NiklasPor Avatar answered Nov 15 '22 11:11

NiklasPor


Disclaimer: I's a total newbie at Dart (about 3 days of juggling and still on the first few pages of the the specs) but coming from C# I've made the simplest test:

class Obj {
    String s1;
}

class Outer {
    Obj obj;

    Outer() {
        obj = Obj();
        obj.s1 = "Stuff";
    }

    void passValueToInner() {
        var inner = Inner();
        print("Obj.s1 = ${obj.s1}");
        inner.changeObj(obj);
        print("Obj.s1 = ${obj.s1}");
    }
}

class Inner {
    void changeObj(Obj obj) {
        obj.s1 = "OtherStuff";
        obj = null;
    }
}

It prints:

Outer.Obj.s1 = Stuff
Obj is null? true
Outer.Obj.s1 = OtherStuff

So, it appears that objects are not really passed by reference in the sense of C/C++ and others. Dart passes a new handle pointing to the object in memory (or managed vector, whatever... newbie remember...) into the called method/function. That's why it acts like byRef if you change it. When you null-it, you are only acting on the handle.

like image 36
CalomanX Avatar answered Nov 15 '22 10:11

CalomanX