On May 3, 2006, at 3:16 PM, [EMAIL PROTECTED] wrote:

On May 03, 2006, at 20:41 UTC, Jason Essington wrote:

Try it!  I would expect the compiler to tell you to take a hike (or
at least, that you've passed an incorrect type on the badFruit call).

Right, that's what I said ... the compiler whines when you try to
pass an apple as a fruit (very silly since an apple is a fruit)

No, it's perfectly happy when you pass an apple as a fruit. What you're trying to do in this example is pass a *reference to an apple* as a *reference to a fruit*. That's not valid, for reasons we've discussed.

In fact, kudos for boiling the array example down to this more simple one, which really gets to the heart of the matter: a reference to a subclass type is NOT a reference to the superclass type. Arrays are just another way of passing around references.

O.K. this is where the real difference lies ... in REALbasic, the array is passes by reference (by default) however in java the reference is passed by value (your head spinning yet?).

So the solution is simply to rewrite our ArrayCopy method to look something like:

sub ArrayCopy(ByVal srcArray() as Object, ByVal destArray() as Object)
  dim l as integer = ubound(srcArray)
  if ubound(destArray) < l then
    // whine loudly about our destination array not being big enough
  end if
  for i as integer = 0 to l
    // check for nil
    // check for cloneable
    destArray(i) = srcArray(i).clone()
  next
end sub

Since we are no longer passing by reference, we can use a more generic object type without the compiler whining, and if we try to stuff an improper type into the array while copying, we'll get the expected IllegalCastException I was describing.

So, my original suggestion is Possible in RB!!! you just have to declare the parameters ByVal, and be sure that the destination array is at least big enough when you pass it in, and everything works as expected.

Whew!

-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