develooper Front page | perl.perl6.language | Postings from July 2006

Re: ===, =:=, ~~, eq and == revisited (blame ajs!) -- Explained

Thread Previous | Thread Next
From:
Darren Duncan
Date:
July 13, 2006 23:53
Subject:
Re: ===, =:=, ~~, eq and == revisited (blame ajs!) -- Explained
Message ID:
p06230901c0dced0212e3@[192.168.1.101]
I think that Jonathan meant for his reply to my message to go to the 
list, so I am including it in its entirety, in my reply.

At 11:23 PM -0700 7/13/06, Jonathan Lang wrote:
>Darren Duncan wrote:
>>Jonathan Lang wrote:
>>>So the purpose of === is to provide a means of comparison that doesn't
>>>implicitly coerce its arguments to a particular type?
>>
>>Yes, absolutely.  The === takes 2 arguments exactly as they are,
>>without changing anything, and says if they are two appearances of
>>the same value.  It would always return false if the 2 arguments are
>>of different declared types.  And if they are of the same types, then
>>no coersion is necessary in order to compare them for equality.
>
>So the difference between eqv and === is:
>
>  @a eqv @b iff all(for each(@a, @b) -> $a, $b { $a === $b }) # a deep
>comparison
>
>or
>
>  @a === @b iff all(for each(@a, @b) -> $a, $b { $a =:= $b }) # a
>shallow comparison
>
>?
>
>That seems counterintuitive to me; I'd rather see both === and eqv
>represent a deep comparison, and leave shallow comparisons to less
>elegant approaches.

I see eqv and === as both being recursive with their own kinds when 
used on any and immutable data types respectively.

Arrays are mutable, so I see that the above examples mean the 
following (only relevant parts changed, other syntax parts may be 
wrong):

   @a eqv @b iff all(for each(@a, @b) -> $a, $b { $a eqv $b })
     # a deep comparison using eqv all along

   @a === @b iff @a =:= @b
     # a shallow comparison since === only tests immutable aspects

Now, lets try two Seq, $a and $b, instead, which are like Array but immutable:

   $a === $b iff all(for each($a.values, $b.values) -> $a, $b { $a === $b })
     # a deep-as-possible comparison using === all along

Assuming that all elements of $a and $b are themselves immutable to 
all levels of recursion, === then does a full deep copy like eqv.  If 
at any level we get a mutable object, then at that point it turns 
into =:= (a trivial case) and stops.

Note that if your Seqs just contain other immutable things like Str 
or Int or Set or Pair or Mapping etc to all recursion levels, which 
they are highly likely to do, then === is simply a deep recursion.

That's how I understand it, and it seems quite elegant and simple.

-- Darren Duncan

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About