Error using `equal` with various string types

Jonathan M Davis jmdavisProg at gmx.com
Sat Feb 23 21:39:31 PST 2013


On Sunday, February 24, 2013 06:21:05 Andrej Mitrovic wrote:
> On 2/24/13, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> > Because the compiler doesn't general deal on the level of code points. It
> > deals with code units, and it doesn't generally treat strings as being
> > special
> > at all. So, you can't compare string and dstring any more than you can
> > compare
> > ubye[] and uint[]. Pretty much the only place in the language where you
> > get
> > 
> > automatic decoding is when you ask for it with a foreach loop by making
> > the
> > 
> > iteration type dchar.
> 
> I'm specifically talking about Phobos, that's why I tried to use
> equals rather than == which doesn't work of course.
> 
> > It just isn't recursive, which is why you're having trouble.
> 
> I don't understand, what does recursion have to do with anything?
> 
> I want these to work:
> 
> "foo".equal("foo".dup);
> "foo".equal("foo"w);
> "foo".equal("foo"w.dup);
> "foo".equal("foo"d);
> "foo".equal("foo"d.dup);

Those _do_ work. The problem is that you're not trying that; you're trying

["foo"].equal(["foo"d]);

You're attempting to compare string[] and dstring[] with equal rather than 
string and dstring.

> Or is there some other function that can be used to compare two
> strings whatever their encoding may be?

The problem is that you're trying to compare string[] and dstring[]. They're 
not comparable with ==, so you tried equal, which means trying to compare 
string and dstring with ==, because that's what the types of front are on 
those ranges, and they aren't comparable with ==.

What I mean by recursive is that you need to keep operating on each inner type 
with equal rather than ==. So, if you have string[][][][][] and dstring[][][]
[][], the only way to do it would be if each level of comparison used equal 
rather than ==. And most everything in Phobos uses ==, not equal. equal is 
only used when you specifically ask for it, and it isn't recursive - it doesn't 
compare each of the layers with equal. It simply compares fronts with ==.

However, if you're only dealing with comparing arrays of strings (e.g. 
string[] and dstring[]) rather than deeper levels (e.g. string[][][][] and 
dstring[][][][]), then giving equal a different predicate should work. So, 
you'd do something like

assert(["foo"].equal!"equal(a, b)"(["foo"d]));

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list