Re: Array comparison
From: Nicolai Hansen (nic_at_aub.dk)
Date: 10/07/04
- Previous message: frederick: "Re: Text in form1"
- In reply to: Rob Kennedy: "Re: Array comparison"
- Next in thread: Skybuck Flying: "Re: Array comparison"
- Reply: Skybuck Flying: "Re: Array comparison"
- Reply: Rob Kennedy: "Re: Array comparison"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 7 Oct 2004 01:50:06 -0700
> Apply the "=" operator to each of the array's elements.
And if the element is an object, you compare addresses, but if its an
integer you compare values?
My reasoning is as follows:
*You cannot compare arrays. "Array of Byte" and "Array of Byte" can
not be compared in Pascal; you need to declare them as type.
*You could then compare your types, but how on earth could you make
something logical that can compare two types if they can compare only
array types, and not record types?
*Next step would be to compare any instance type with another instance
of the same type.
-All strings can be compared to eachother (compares the contents of
the string, but not the reference counts)
-All TObjects can be compared (shallow comparison, compares only the
address of the objects)
*What to do with all other types? Deep or shallow comparison? Deep and
recursive comparison (fear the cross references!)? Who is to tell me
how my records should be compared?
> I think I have to side with Skybuck on this one, and I don't need to
> tell you how much it pains me to do that.
:) I must then prove you wrong! ;)
> > It would then also need to give you with a method of comparing any
> > kinds of records, or classes. And how do you compare two classes? What
> > if they contain pointers? What if they contain _untyped_ pointers?
>
> The only valid point in that is records. The compiler already knows how
> to compare everything else, although some of the comparisons (e.g.,
> objects, pointers) aren't always the most useful. But that the compiler
> doesn't know how to provide an intelligible comparison of two objects
> does not explain why it can't compare two like-typed arrays.
You cannot allow comparisons of certain user-defined types, but not of
others.
The fun part here is that IIRC two identical types can be compared
according to the Pascal ISO.
> > and not even that can count for untyped pointers.
>
> Untyped pointers are very easy to compare. People do it all the time.
>
> var
> a, b: Pointer;
>
> if a = b then ...
Yes, it is easy to compare the value of the pointers. IE compare them
as if they were integers... But this might not be what was wanted.
> > The only way in Pascal is to write your own procedures (you actually
> > do that in C++ also), like function IsEqual(comp1, comp2: TSameType):
> > boolean - you can overload this as much as you want to make it compare
> > any kind of types; make an IsEqual(comp1, comp2: Pointer) or
> > IsEqual(comp1, comp2: TObject) to cover for all objects where you only
> > want addresses compared...
>
> Writing generic procedures to compare static arrays and records was a
> cool little exercise. Tomorrow I'll tackle how to do a deep comparison
> on dynamic arrays.
>
> Whatever the reasons for disallowing "=" on static arrays and records,
> technical difficulty is not one of them -- not nowadays, anyway. I'm
> more inclined to say the reason is lack of customer demand for the feature.
I never said it was a technical difficulty in comparing arrays; I
stated that it was plain easy to write your own compare methods. My
point was that I see reason in it NOT being allowed, due to confusion
about which types can and can not be compared.
- Previous message: frederick: "Re: Text in form1"
- In reply to: Rob Kennedy: "Re: Array comparison"
- Next in thread: Skybuck Flying: "Re: Array comparison"
- Reply: Skybuck Flying: "Re: Array comparison"
- Reply: Rob Kennedy: "Re: Array comparison"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|