realbasic-nug
[Top] [All Lists]

Re: Operator_Compare with variant objects?

To: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Subject: Re: Operator_Compare with variant objects?
From: Joe Strout <joe@inspiringapps.com>
Date: Sat, 30 May 2009 14:35:52 -0600
Authentication-results: mx.google.com; spf=neutral (google.com: 74.124.194.228 is neither permitted nor denied by best guess record for domain of realbasic-nug-bounces@lists.realsoftware.com) smtp.mail=realbasic-nug-bounces@lists.realsoftware.com
Delivered-to: listarchive@realsoftware.com
In-reply-to: <9142A64B-6ACF-4072-B847-82A5C7098969@xochi.com>
Organization: Inspiring Applications, Inc.
References: <AEF09038-DB63-4857-BBE1-AE5D13AA6FF3@xochi.com> <3AA0AEC2-D230-4A4C-8788-84C3FCC17A44@great-white-software.com> <9142A64B-6ACF-4072-B847-82A5C7098969@xochi.com>
Reply-to: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Sender: realbasic-nug-bounces@lists.realsoftware.com
User-agent: Thunderbird 2.0.0.21 (Macintosh/20090302)
Michael Diehr wrote:

     dim o1,o2 as object
     o1 = v1.objectValue
     o2 = v2.objectValue
     if o1 = o2 // still doesn't call the Operator_Compare

That's because Object doesn't have an Operator_Compare.

Keep in mind that comparisons are compiled at, er, compile time. So, for a comparison operator to be invoked, it must be something the compiler can know about at that time -- i.e., something defined on the static type of the object references involved.

The only way I can make it work is to explicitly cast to the object class, but that's inelegant as it is no longer generic code and requires a giant Case statement:

  if v1.type = variant.TypeObject then
    select case v1.objectValue
    case isa cClass1
       dim o1 as cClass1 = v1
       dim o2 as cClass1 = v2
       if o1 = o2 // NOW we get the the Operator_Compare() called
    case isa cClass2
       [...]
    case isa cClass3
       [...]

True enough. And even this giant case statement only makes sense because you are assuming that v1 and v2 hold instances of the same class (an assumption the compiler would have no way of knowing, even if it's true).

But you could do something like this: define an interface, say Comparable, that defines a Compare method (or IsEqual or whatever you want to call it) that takes another Comparable as a parameter. Have all your various classes that you want to do this trick with implement the Comparable interface. Then, typecast v1 to a Comparable, and pass it v2, and it's done.

(It will of course be up to the particular implementations to decide what to do when they're asked to compare themselves against some completely different class of object.)

Best,
- Joe

--
Joe Strout
Inspiring Applications, Inc.
http://www.InspiringApps.com


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>


<Prev in Thread] Current Thread [Next in Thread>