For Programmers: Free Programming Magazines  


Home > Archive > Tcl > January 2008 > Re: Why doesn't foreach return a value









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Why doesn't foreach return a value
Gerald W. Lester

2008-01-30, 7:46 pm

Fredderic wrote:
> On Sat, 17 Nov 2007 11:52:37 -0800 (PST),
> Mark Janssen <mpc.janssen@gmail.com> wrote:
>
>
> It is an interesting point, though... More of the control structures
> could easily return a value. I'm not entirely certain how often it'd
> actually be useful, but it might be interesting. Of more use, I think,
> would be if [break] and [continue] could return an argument. Have them
> stash it away in the options dict so you can distinguish between an
> empty value and no value at all.


You can effectively have a "break" or "continue" return a value, but doing:

proc break {{value {}}} {
return -code break -level 2 $value
}

proc continue {{value {}}} {
return -code continue -level 2 $value
}

The catch command returns the status code and takes an optional argument to
take the return value. As in:
set status [catch {break foo} bar]
puts stdout "Status = {$status}, return value = {$bar}"


--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
Donald G Porter

2008-01-30, 7:46 pm

Gerald W. Lester wrote:
> proc break {{value {}}} {
> return -code break -level 2 $value
> }
>
> proc continue {{value {}}} {
> return -code continue -level 2 $value
> }


Um, those are wrong. Drop the -level arguments.

--
| Don Porter Mathematical and Computational Sciences Division |
| donald.porter@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|_______________________________________
_______________________________|
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com