Difference between revisions of "RefEquals"

From MiniScript Wiki
Jump to navigation Jump to search
(Created page with "The <c>refEquals</c> function checks two values for reference equality. This is different from the <c>==</c> operator, which checks whether two values are ''equivalent'' (e.g...")
 
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
  
 
Whenever <c>refEquals(a, b)</c> is true (1), then you know that any modification of <c>a</c> will also be seen via <c>b</c>, because <c>a</c> and <c>b</c> refer to the same object.
 
Whenever <c>refEquals(a, b)</c> is true (1), then you know that any modification of <c>a</c> will also be seen via <c>b</c>, because <c>a</c> and <c>b</c> refer to the same object.
 +
 +
For immutable types – <c>null</c>s, <c>number</c>s and <c>string</c>'s – <c>refEquals</c> returns the same result as the <c>==</c> operator.
  
 
=== Arguments ===
 
=== Arguments ===
Line 24: Line 26:
 
print refEquals(a,b)  // prints 1, as these are the same map
 
print refEquals(a,b)  // prints 1, as these are the same map
 
print refEquals(a,c)  // prints 0, as these are different maps
 
print refEquals(a,c)  // prints 0, as these are different maps
 +
 +
print refEquals(42,42)// prints 1
 +
nan = 0/0
 +
print refEquals(nan,nan)//prints 0
 
</ms>
 
</ms>
  
 
[[Category:Intrinsic Functions]]
 
[[Category:Intrinsic Functions]]
 
[[Category:Numeric Functions]]
 
[[Category:Numeric Functions]]

Latest revision as of 08:13, 17 November 2023

The refEquals function checks two values for reference equality. This is different from the == operator, which checks whether two values are equivalent (e.g., two maps that contain equal key/value pairs, or two lists which contain equal values).

Whenever refEquals(a, b) is true (1), then you know that any modification of a will also be seen via b, because a and b refer to the same object.

For immutable types – nulls, numbers and string's – refEquals returns the same result as the == operator.

Arguments

Parameter Name Default Value Meaning
a any value first argument
b any value second argument


Example

a = {"foo":42}
b = a
c = {"foo":42}
print a == b          // prints 1, because a and b have the same value
print a == c          // prints 1, because a and c have the same value
print refEquals(a,b)  // prints 1, as these are the same map
print refEquals(a,c)  // prints 0, as these are different maps

print refEquals(42,42)// prints 1
nan = 0/0
print refEquals(nan,nan)//prints 0