Task

Show how to return more than one value from a function.




/* NetRexx */
options replace format comments java crossref symbols nobinary
 
-- =============================================================================
class RReturnMultipleVals public
  properties constant
    L = 'L'
    R = 'R'
    K_lipsum = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
    K_1024 = 1024
 
  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  method RReturnMultipleVals() public
    return
 
  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  method main(args = String[]) public static
    arg = Rexx(args)
    parse arg nv_ sv_ .
    if \nv_.datatype('n') then nv_ = K_1024
    if sv_ = '' then sv_ = K_lipsum
 
    lcl = RReturnMultipleVals()
 
    rvr = lcl.getPair(nv_, sv_) -- multiple values returned as a string.  Use PARSE to extract values
    parse rvr val1 val2
    say 'Results extracted from a NetRexx string:'
    say val1',' val2
    say
 
    rvr = lcl.getPairFromRexx(nv_, sv_) -- values returned in a NetRexx indexed string
    say 'Results extracted from a NetRexx "indexed string":'
    say rvr[L]',' rvr[R]
    say
 
    rvp = lcl.getPairFromPair(nv_, sv_) -- values returned in a bespoke object
    say 'Results extracted from a composite object:'
    say rvp.getLeftVal',' rvp.getRightVal
    say
 
    rvl = lcl.getPairFromList(nv_, sv_) -- values returned in a Java Collection "List" object
    say 'Results extracted from a Java Colections "List" object:'
    say rvl.get(0)',' rvl.get(1)
    say
 
    rvm = lcl.getPairFromMap(nv_, sv_) -- values returned in a Java Collection "Map" object
    say 'Results extracted from a Java Colections "Map" object:'
    say rvm.get(L)',' rvm.get(R)
    say
 
    return
 
  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  -- returns the values in a NetRexx string.
  --  Caller can the power of PARSE to extract the results
  method getPair(nv_, sv_) public returns Rexx
    return nv_ sv_
 
  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  -- Return the values as members of a NetRexx indexed string
  method getPairFromRexx(nv_, sv_) public returns Rexx
    rval = ''
    rval[L] = nv_
    rval[R] = sv_
    return rval
 
  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  -- Return a bespoke object.
  -- Permits any number and type of value to be returned
  method getPairFromPair(nv_, sv_) public returns RReturnMultipleVals.Pair
    rset = RReturnMultipleVals.Pair(nv_, sv_)
    return rset
 
  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  -- Exploit Java Collections classes to assemble a collection of results.
  -- This example uses java.util.List
  method getPairFromList(nv_, sv_) public returns java.util.List
    rset = ArrayList()
    rset.add(nv_)
    rset.add(sv_)
    return rset
 
  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  -- This example uses java.util.Map
  method getPairFromMap(nv_, sv_) public returns java.util.Map
    rset = HashMap()
    rset.put(L, nv_)
    rset.put(R, sv_)
    return rset
 
-- =============================================================================
class RReturnMultipleVals.Pair dependent
 
  properties indirect
    leftVal
    rightVal
 
  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  method Pair(nv_ = parent.K_1024, sv_ = parent.K_lipsum) public
    setLeftVal(nv_)
    setRightVal(sv_)
    return
 