develooper Front page | perl.perl5.porters | Postings from June 2015

Re: scalar sort $scalar

Thread Previous | Thread Next
June 30, 2015 14:58
Re: scalar sort $scalar
Message ID:
Ed Avis wrote:
>Now, whether these are useful things to have in core is another matter.
>When you write a Perl regular expression you are plugging in to some highly
>optimized string handling code,

Yeah.  Regexps on general lists don't have anything like that win, and
would not naturally sit as an extension of the string regexp system.
They'd be a parallel construction, with no particular reason to be
in core.

>                                                         99% of the time I
>am happy to use string equality, as 'eq'.  That handles strings and it
>handles references,

You have a problem there: in a lot of applications we'd like to recognise
["a"] and ["a"] as equivalent, even though they're references to separate
arrays.  So even if ordinary scalars aren't a problem, you still need
a more sophisticated concept of value identity.  In practice you need
different kinds of comparison for different purposes.  This is part of
why I suggested using type constraints: a type metaobject encapsulates
some kind of comparison, and different kinds can coexist.

There's another reason to use type metaobjects too: most interesting
list regexps need to accept an infinite range of values at some point.
My Lisp has an excellent concept of value identity, but I still use type
metaobjects for this purpose.

The downside of types as the atomic expressions is that things like your
('..'+) example pattern don't naturally work: you need to construct a
type metaobject that encapsulates the concept of "equal to '..' under
whichever comparison I want".  However, a simple literal like '..' could
be shorthand for such a type class using some suitable default comparison
operator, and if that default is actually smartmatch then there can be
some uniformity of treatment between simple literals and type metaobjects.
(Moose's type constraint objects don't do ~~, but they could be made to.)


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About