On May 3, 2006, at 10:06 AM, [EMAIL PROTECTED] wrote:

On May 03, 2006, at 15:17 UTC, Jason Essington wrote:

BAH! I forgot, RB doesn't have a proper Object that is the base of
all objects :-(

No, that has nothing to do with it. You can declare an array of type Object. But that simply doesn't help. Let's use the classic "fruits" hierarchy to see why:

Suppose you have a method that can operate on an array of Fruits. Perhaps all fruit objects have a Sweetness property, and this method sorts fruits by their sweetness:

Sub SortBySweetness( fruits() as Fruit )

That's fine, but now suppose you have elsewhere in your code an array declared as:

  Dim apples() as Apple

An Apple is a Fruit, so you can pass this to your SortBySweetness method, right? Wrong. The SortBySweetness code knows only that that its array is defined to contain fruits, so it would be perfectly valid for it to do something like:

  fruits.Append New Orange

Caution! The following is just how I would expect things to work, and is absolutely not how RB works!

Actually not, since the array of apples passed to a method as an array of fruits is still an array of apples, any attempt to append a fruit that is not an apple should simply raise an IllegalCastException at runtime.

In this case an Orange is a fruit, but it is definitely not an Apple so the exception is a perfectly reasonable thing to expect.

This is conceptually similar to doing something like:

Sub badFruit(ByRef f As Fruit)
   f = new Orange()
End Sub

Dim a as new Apple()
badFruit(a)

Rather than getting a compile exception at badFruit(a), I would expect to get an IllegalCastException at f = new Orange().

Now, if you could pass your apples array in, you'd end up with an array declared to contain only Apples, but actually containing an Orange. The result would be a crash.

Not if the assignment (append) failed when trying to cast the fruit to the proper type.

So you can see, just because an Apple IsA Fruit, does NOT mean that an array of Apples IsA array of Fruits.

B-B-B-But an array of Apples IsA array of Fruits! A rather specific array of Fruits, but still an array of Fruits.

That's why you can't make a generic CloneArray method that would work on any array of Cloneable objects, and is probably why RB doesn't contain such functionality.

Well, conceptually, it should be as simple as something like:

Sub CloneArray(oldArray() As Object, newArray() As Object)
  Redim newArray(0)
  For Each o As Object in oldArray
    If Not o IsA Cloneable Then
      msgbox "To clone an array, all of the elements must be Cloneable"
      Exit
    End If
    newArray.Append(o.clone())
  Next
Exception IllegalCastException
  msgBox "Your arrays are not entirely compatible"
End Sub

It has to be written for the specific type care about. (It's also why most of the methods in my open-source ArrayUtils module don't operate on object arrays, even when it might naively seem like they could.)

Well, It isn't naivety that makes me think that it should work, it is that it does work exactly that way in my primary language (Java).

So, unfortunately, what I thought was a fairly simple problem, in REALity ends up being a bit more complicated, but with the benefit that it is much harder to shoot yourself in the foot.

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

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to