Ways to return values

Difference between version 1 and 2 - Previous - Next
======
# Testing flexible return of lots of values without temporary accumulating them locally
#
proc test {args} {
     set i 0
     set args [dict merge [dict create cmd {} somethingelse abc nochwas def] $args]
     # Returning some example Values
     foreach {id path} {1 test1 2 test2 3 test3} {
          uplevel [format [dict get $args cmd] $id $path]
          incr i
     }
     return $i
}

proc callback {arg1 arg2} {
     puts "$arg1 $arg2"
}

puts [test cmd {lappend liste %s %s}]; # building a list
puts $liste

puts [test cmd {set arr(%s) %s}]; # building an array out of the values
parray arr

puts [test cmd {callback %s %s}]; # calling a callback to do something else
puts [test cmd {puts "Test: %s %s"}]; # outputting (another callback)

puts [test cmd {dict set d %s %s}]; # building a dictionary
puts $d

# A wrapper (but overhead)
proc testAsList {} {
     test cmd {lappend liste %s %s}
     return $liste
}

puts [testAsList]

# Alternative: specifying type of return and rewriting own proc accordingly (to be cont'd later)

# Testing coros
puts [coroutine t test cmd {yield "%s %s"}]; # despite of UPLEVELing yield, it works :-D
puts [t]
puts [t]
puts [t]
puts "[catch {puts [t]} ret] $ret"


======
[pyk] 2019-11-20:  See also, [Iterator Protocol] and [ycl%|%ycl coro call].