Skip to content

How is mutation of passed variable possible? #1983

Answered by st-pasha
Chappie74 asked this question in Q&A
Discussion options

You must be logged in to vote

In Dart, only primitive types are passed by values: booleans, integers, doubles. Every other object is passed "by reference". For example, if I have a card object, and pass it to someFunction(card), then the function will see exactly the same object as known to the caller. If this was C++, then the semantics would be expressed as auto someFunction(Card& card).

If someFunction modifies the card object, those changes will be visible to the caller -- because it's just the same object.

In Dart there is no notion of passing objects "by value" -- if you want to create an object copy, you would have to manually define and call the Card.clone() method. Neither there is a way to pass an object by …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Chappie74
Comment options

Answer selected by Chappie74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants