develooper Front page | perl.perl6.language | Postings from August 2005

~ and + vs. generic eq

Thread Next
From:
Yuval Kogman
Date:
August 23, 2005 08:20
Subject:
~ and + vs. generic eq
Message ID:
20050823151933.GE2318@woobling.org
I don't like eqv, because it's ugly, inconsistent with anything else
in Perl 6, especially &&, ||, and ^^. It might be forced to fit into
the and, or, and xor family, but you'd expect to find 'eq' there,
and that's not what it means.

IMHO == is as "generic" as && and ||, and is even more like ^^ since
it also returns a boolean.

the v in eqv is not much of a mnemonic, and overall looks like a
typo more than a meaningful distinction.

Furthermore, people culturally lean towards '==' for generic
equality, and that's what 99% of 'use overload' uses choose on the
CPAN.

Moreover, after a while of programming only "real" apps, that is,
few throw-away scripts (i think it was a period of 2-3 months) I
suddenly found myself comparing strings with == in such scripts.

I think this is more consistent, and just as useful:

	10 == 10; # dispatches to num
	"10" == 10; # dispatched to Num, by means of coercion (== has some affinity to it for backwards compatibility)
	"10" == "10"; # dispatches to Str, due to better match
	"10.0" == "10"; # unlike perl 5 this is false
	"10.0" +== "10"; # but this is true
	10 ~== 10; # like eq

	$obj == $obj; # if $obj.ref implements ==, then goody, if not, i guess it numifies, or falls back to =:= or whatever identity equality is this week

	$obj eq $obj; # just a wrapper for == that is parsed with lower precedence

	$obj +== $obj; # numifies
	$obj ~== $obj; # stringifies

	@array +== @array; # length
	@array == @array; # nested

Here are some reference implementations:

	sub &infix:<+==> (Any $l, Any $r --> Bool) { +$l == +$r }
	sub &infix:<~==> (Any $l, Any $r --> Bool) { ~$l == ~$r }
	sub &infix:<?==> (Any $l, Any $r --> Bool) { ?$l == ?$r } # useless?

	multi sub &infix:<==> (Num $l, Num $r --> Bool) { ... }
	multi sub &infix:<==> (Str $l, Str $r --> Bool) { ... }

	multi sub &infix:<==> (@l, @r --> Bool) { @l.elems +== @r.elems and @l >>==<< @r }

	sub &infix:<eq> (Any $l, Any $r) is tighter(&infix:<and>) { $l == $r }

The behavior of == is akin to ~~, for example regarding:

    Array   Array     arrays are identical     match if $_ >>=<< @x

extracted from S04.

The "matchic MMD generator" in the prelude, that makes the complex
set of rules found in s04 should simply apply to any infix operator
(and should be applied in the prelude to ~~, as well as ==), so that
~~ and == on collections are defined with the same aggregate
semantics, but MMD ~~ or == applies to the nested elements in the
same "shape":

	sub extend_comparators (&op) {
		...
		
		my sub apply_to_arrays (Code &op, Array @l, Array @r) {
			@l >>&op<< @r
		}

		&op.add_mmd_variant(&apply_to_arrays.assuming(&op);
	}
	
	for (&infix:<~~> &infix:<==>) &extend_comparators;

-- 
 ()  Yuval Kogman <nothingmuch@woobling.org> 0xEBD27418  perl hacker &
 /\  kung foo master: /me whallops greyface with a fnord: neeyah!!!!!!!



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