-1

I'm confused,,who can explan it to me ,my code:

int fun1() {
  return 1;
}

int fun2(int x) {
  return x + x;
}

void main() {
  print(identical(1, fun1()));
  print(identical(2, fun2(1)));
}

result: true true

my question: why const 1 and fun1() are the same object const 2 and fun2() are the same object ?? thanks

  • All of the primitive types are immutable, and therefore are created as identical objects if they are reasonably equal. This includes symbols: `#foo` is the same object as `#foo` everywhere. – Randal Schwartz Apr 06 '21 at 04:28
  • thank you for your answer,it makes me a little clear,,,,but the dart doesnt differ the object created in or out of a function when it is aprimitive type? – 福泽谕吉 Apr 06 '21 at 06:52

1 Answers1

0

It's because you executed fun1() which returned 1 and now you comparing 1 and 1 which is obviously true and true.

Vansh
  • 11
  • 6
  • `identical` doesn't compare just for equality, but for being exactly the same object. – bereal Apr 05 '21 at 08:20
  • what about fun2? does it means that if there is a object of the same value ,then the function will return a reference to that object without building a one? – 福泽谕吉 Apr 05 '21 at 10:22