Is str ~ regex the root of all evil, or the leaf of all good?

Michel Fortin michel.fortin at michelf.com
Thu Feb 19 04:13:53 PST 2009


On 2009-02-19 00:50:06 -0500, Bill Baxter <wbaxter at gmail.com> said:

> On Thu, Feb 19, 2009 at 2:35 PM, Andrei Alexandrescu
>> In general I'm weary of unwitting operator overloading, but I think this
>> case is more justified than others. Thoughts?
> 
> No.  ~ means matching in Perl.  In D it means concatenation.  This
> special case is not special enough to warrant breaking D's convention,
> in my opinion.  It also breaks D's convention that operators have an
> inherent meaning which shouldn't be subverted to do unrelated things.

Indeed. That's why I don't like seeing `~` here.


> What about turning it around and using 'in' though?
> 
>    foreach(e; regex("a[b-e]", "g") in "abracazoo")
>       writeln(e);
> 
> The charter for "in" isn't quite as focused as that for ~, and anyway
> you could view this as finding instances of the regular expression
> "in" the string.

That seems reasonable, although if we support it it shouldn't be 
limited to regular expressions for coherency reasons. For instance:

	foreach(e; "co" in "conoco")
		writeln(e);

should work too. If we can't make that work in the most simple case, 
then I'd say it shouldn't with the more complicated ones either.

By the way, regular expressions should work everywhere where we can 
search for a string. For instance (from std.string):

	auto firstMatchIndex = find("conoco", "co");

should work with a regex too:

	auto firstMatchIndex = find("abracazoo", regex("a[b-e]", "g"));

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list