1. RE: pass by reference Karl

kbochert at ix.netcom.com wrote:
> -------Phoenix-Boundary-07081998-
> Content-type: text/plain; charset=ISO-8859-1
> Content-transfer-encoding: 8bit
> 
> Hi Irv Mullins, you wrote on 2/12/02 10:26:37 AM:
> 
> >If I call foo(integer x) as follows:
> >
> >x = 3
> >foo(x)
> >? x => 3
> >
> >No matter what happens inside the mysterious foo, x is still what it was
> >before calling the function.
> >Even if foo sets x to 99, it's only 99 while inside the foo routine.
> >So foo is working with a copy of x, not the real x.
> >
> >Pass-by_reference passes the actual variable, not a copy, so that
> >foo(var integer x)
> > x = 99
> >end
> >
> >x = 3
> >foo(x)
> >? x  => 99
> >
> >The variable IS changed within the routine, and remains changed 
> >afterward.
> >
> >Regards,
> >Irv
> 
> In my implementation, pass-by reference is accomplished by
> the caller. That is:
> 
>    procedure foo (sequence x)
>       x = x[2..3]
>    end procedure
> 
>    sequence s = "test"
>    foo (s)
>    -- s still equals "test"
> 
>    foo (!!s)
>    -- s now equals "es"
> 
> Library routines can never do something behind your
> back.
> 
> The other not-so-minor detail is that only sequences
> can be passed by reference.


Karl:
  What happens when a user does slicing, append, prepend
  on the sequence that is passed. Don't forget that
  sequences are dynamic and are reallocated dynamically. 
Bernie

new topic     » topic index » view message » categorize

2. Re: RE: pass by reference Karl

kbochert at ix.netcom.com wrote:
> 
> In my implementation, pass-by reference is accomplished by
> the caller. That is:
> 
>    procedure foo (sequence x)
>       x = x[2..3]
>    end procedure
> 
>    sequence s = "test"
>    foo (s)
>    -- s still equals "test"
> 
>    foo (!!s)
>    -- s now equals "es"
> 
> Library routines can never do something behind your
> back.

I'm not so sure Karl. Lets say that I have a routine like this...

   procedure X(object a)
      if sequence(a) then
          a = ConvertToNumber(a)
      end if

      GlobalX += a
   end procedure

then I call this using your method...

   s = "23"
   X(!!s)

this should either crash with an assignment error (assigning an atom to seq) or
change the sequence
S to now be an atom.

However, calling it normally is quite safe...

   s = "23"
   X(s)

---------
Cheers,
Derek Parnell

new topic     » goto parent     » topic index » view message » categorize

3. RE: pass by reference Karl

-------Phoenix-Boundary-07081998-

Hi Bernie Ryan, you wrote on 2/12/02 1:20:32 PM:

>
>Karl:
>  What happens when a user does slicing, append, prepend
>  on the sequence that is passed. Don't forget that
>  sequences are dynamic and are reallocated dynamically.
>Bernie
>

Thanks for pointing that out -- I had overlooked it totally.
The good news is that I have now managed to correct the
slicing case:
   procedure foo (sequence s)
     s = s[3..]
   end procedure

   sequence t = "Hello"
   foo (!!t)
   ?t       -- => "llo"

Time and again I run into these issues that seem to
be killers, only to discover that the code to do
them seems to be already there. I think Rob did NOT
choose pass-by-value because it was the easy
choice for implementation.

My motivation for adding pass-by-value is to allow
class methods to be passed a 'this reference' which
refers to the instance to be modified. My original
implementation accomplishes this nicely because no
class method ever changes the structure of the instance.

Given the ease of handling slices, I suspect that
all cases could be handled, but the risk of deep
problems grows, so I am somewhat reluctant to
proceed.

Thanks for the bug find
Karl

-------Phoenix-Boundary-07081998---

new topic     » goto parent     » topic index » view message » categorize

4. Re: RE: pass by reference Karl

-------Phoenix-Boundary-07081998-

Hi Derek Parnell, you wrote on 2/12/02 4:21:17 PM:

>
>kbochert at ix.netcom.com wrote:
>
> In my implementation, pass-by reference is accomplished by
> the caller. That is:
>
>    procedure foo (sequence x)
>       x = x[2..3]
>    end procedure
>
>    sequence s = "test"
>    foo (s)
>    -- s still equals "test"
>
>    foo (!!s)
>    -- s now equals "es"
>
> Library routines can never do something behind your
> back.
>
>I'm not so sure Karl. Lets say that I have a routine like this...
>
>   procedure X(object a)
>      if sequence(a) then
>          a = ConvertToNumber(a)
>      end if
>
>      GlobalX += a
>   end procedure
>
>then I call this using your method...
>
>   s = "23"
>   X(!!s)
>
>this should either crash with an assignment error (assigning an atom to
>seq) or change the
>sequence
>S to now be an atom.
>
>However, calling it normally is quite safe...
>
>   s = "23"
>   X(s)
>
>---------
>Cheers,
>Derek Parnell
>

   I hadn't considered that possibility.
Surprisingly it seems to work reasonably!

   integer q = 1
   sequence t = "hello"

procedure X(object a)
   if sequence(a) then
      a = 5
   end if
   q += a
   ?q        --=> 6
end procedure

   X (!!t)
   ?t       -- => "hello"

The procedure never actually modifies the
sequence, only the identifier. the 'a=5'
line creates a new entity. A bit confusing
perhaps but what can you do? I guess that
PBR only happens when you pass a sequence AND the
subroutine treats it as a sequence.

Karl

-------Phoenix-Boundary-07081998---

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu