[comp.sys.mac.hypercard] passing var parameters in HyperTalk

KOFOID@cc.utah.edu (08/10/90)

HyperTalk does not allow parameter passing by reference. If I understand
correctly, version 2 will be the same. This makes it difficult to write
generalized handlers for modular use.

I can imagine two ways to do this, but I'm not sure either is possible:

1) Pass a handle to the container being used as a parameter and dereference it
within the script body. The problem is finding the handle. There is no
legitimate way in HT of determining this.

2) Pass the body of a script to an XCMD, which then executes it and sets the
appropriate containers to values calculated within the script. However, the
available glue routines only allow XCMDs to execute singe HT expressions. I am
not at all sure how one could extend this to the execution of a script body,
without first writing an entire HT interpreter, which essentially recreats
HyperTalk within an XCMD!

Below, I offer a demonstration that the effect of var parameters can be
kludged. It is ugly. But, it allows the creation of a library of modular
scripts with general utility. There must be a better way!

--********************* Start Demo *********************--

               --===== stack scripts =====--


function varComLine varName,var

  -- Utility: adds to the growing var command line 
  -- within a function passed var parameters.

  return "put"&&var&&"into"&&varName&return
end varComLine

function setVars index,comLine

  -- Utility: sets local vars to values 
  -- last computed within function.

  return line index of comLine
end setVars


               --===== test button scripts =====--


function test varName1,var1,varName2,var2

  -- Sample routine begin passed pseudo-var parameters.
  -- Note clumsy parameter line: the quoted name of each
  -- parameter must be passed, in addition to its current
  -- value.

  -- Do something with the first parameter. For instance:
  put (2*var1) -1 into var1
  put varComLine(varName1,var1) into comLine

  -- Do something with the second parameter. For instance:
  put (var2/3)+1000 into var2
  put varComLine(varName2,var2) after comLine

  return comLine
end test

on mouseUp

  -- Get some input for the test:
  ask "type a number"
  put it into theValue1
  ask "type a number"
  put it into theValue2

  -- Call the test routine with its pseudo-var parameters
  -- and save the instruction string it passes:
  put test("theValue1",theValue1,"theValue2",theValue2) into theVars
  
  -- Reset the local variables:
  repeat with i=1 to number of lines in theVars
    do setVars(i,theVars)
  end repeat
  
  -- Prove that the values of local vars have changed:
  put "theValue1="&theValue1&&"theValue2="&theValue2
end mouseUp

--********************* End Demo *********************--


Cheers,

Eric.

 __________________________________________________________________
|                          (801) 581-3592                          |
|  Snail: Eric Kofoid; Dept. Biology, U. of Utah; SLC, UT 84112    |
|   Fast: bi.kofoid%science@utahcca (BitNet)                       |
| Faster: bi.kofoid@science.utah.edu (InterNet)                    |
|Fastest: kofoid@bioscience.utah.edu (InterNet -> QuickMail)       |
|                                                                  |
| -- The University of Utah is blameless for anything I've said -- |
|__________________________________________________________________|

gannholm@apple.com (Martin Gannholm) (08/11/90)

In article <84042@cc.utah.edu> KOFOID@cc.utah.edu writes:
> 2) Pass the body of a script to an XCMD, which then executes it and sets 
the
> appropriate containers to values calculated within the script. However, 
the
> available glue routines only allow XCMDs to execute singe HT 
expressions. I am
> not at all sure how one could extend this to the execution of a script 
body,
> without first writing an entire HT interpreter, which essentially 
recreats
> HyperTalk within an XCMD!

In HyperCard 2.0, the "do" command can take multi-line scripts, even 
containing repeat loops and if statements. I'm not sure this would do 
exactly what you want, but it is useful in many situations.

Martin Gannholm
Apple Computer

Exclaimer !!! I typed this myself, therefore I am myself !