Front page | perl.perl6.language |
Postings from August 2005
Re: ~ and + vs. generic eq
Thread Previous
From:
Yuval Kogman
Date:
August 23, 2005 10:10
Subject:
Re: ~ and + vs. generic eq
Message ID:
20050823170948.GF2318@woobling.org
On Tue, Aug 23, 2005 at 18:15:07 +0200, Ingo Blechschmidt wrote:
> sorry, I've some problems with this proposal:
>
> == has always meant numeric equality in Perl and I'd like it to stay
> that way.
For "simple" values like numbers and strings == is numberic, because
it's affinity to it.
<snip>
> > "10" == 10; # dispatched to Num, by means of coercion (== has some
> > affinity to it for backwards compatibility)
</snip>
I guess the difference is that this will fallback to dispatching to
strings if "10" can't be numified.
I think what sets perl 6 apart from perl 5 is that it's 95% as
optimized for "simple" values as perl 5 was, but a magnitude times
better for complex types (first class functions, better objects),
and in this sense generic equality is a place I find the current
spec lacking.
> More importantly, the distinction between +== and ~== is far too subtle,
> I think -- the two long =s kind of hide the +~?. You have to look more
> carefully to see the distinction between +== and ~==; == vs. eq is much
> more apparent, IMHO.
Arguably it should only matter when it's explicitly stated, so in
this case I think your argument is very strong. The only difference
is that I don't feel that ~== and +== are very close to == visually,
and although they are hard to distinguish, they do stand apart from
just bare ==.
>
> Oh and +== isn't as good huffmanized as == is.
99% of the time you should use == for numbers anyway... It's
unambiguous.
> > 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) {
> [...]
>
> I think I like that idea, although I have to admit that I might have not
> understood it fully.
>
> I wonder whether it's possible to use an adverbial modifier to specify
> the comparator:
>
> say @foo ~~ @bar; # is really
> say @foo ~~ @bar :comparator{ $^a ~~ $^b };
>
> say @foo ~~ @bar :comparator{ $^a eq $^b };
> say @foo ~~ @bar :comparator{ $^a == $^b };
In this example I would say
my sub comparator ($a, $b) { .. your comparison here };
Prelude::extend_comparators(&comparator);
say comparator(@foo @bar);
and the name "comparator" could be a lexically scoped &infix:<~~> just the
same.
It's not as short, but I'm sure an evil macro could be cooked up if
it recurrs.
We can even specify
multi &infix:<~~> (Any $a, Any $b, :&comparator) { # what is the
# syntax for mandatory positionals nowadays?
my &augmented = &comparator; # a copy
Prelude::extend_comparators(&augmented);
&augmented($a, $b);
}
And have your interface wrap around this behavior.
--
() Yuval Kogman <nothingmuch@woobling.org> 0xEBD27418 perl hacker &
/\ kung foo master: /me beats up some cheese: neeyah!!!!!!!!!!!!!!!!!
Thread Previous