Reputation: 1403
I have a class Field and has a copy() method, I want to pass self reference to new object but Dart editor seem not allow. How can I pass self reference to new object like java?
class Field
Field copy() {
return new Field(this);
}
}
Upvotes: 2
Views: 5404
Reputation: 21383
You seem to be missing an opening curly brace and you don't have a constructor. Other than that, there's no reason why this shouldn't work:
class Field {
Field ref;
Field(this.ref);
Field copy() {
return new Field(this);
}
}
Field a = new Field(null);
Field b = a.copy();
print(identical(a, b.ref)); // true
Upvotes: 1
Reputation: 76223
There's no problem to do such things :
class Field {
String name;
Field(Field other) {
// init current with other
}
Field copy() => new Field(this);
}
Upvotes: 2