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
Mark Janssen

2008-01-30, 7:46 pm

On Jan 30, 7:01 pm, "tom.rmadilo" <tom.rmad...@gmail.com> wrote:
> On Jan 30, 6:02 am, "Gerald W. Lester" <Gerald.Les...@cox.net> wrote:
>
>
>
>
>
>
> Of course the problem is what? You want [foreach] to return a value?
> No, wait, you want [break] and [continue] to set the return value of a
> [foreach] loop? What happens if [foreach] finishes without a [break]
> or [continue]?
>
> While we are at it, why not have [break] or [continue] return a value?
> That is what the above code does. And of course it doesn't work
> either:
>
> % foreach b {e f g} {break $b}
> invoked "break" outside of a loop
>
> Maybe just admit that although there are shared syntactic features,
> not all Tcl commands are the same. foreach/for/while are commands in
> Tcl, but they are just language features in most programming
> languages. This is purely syntax, they are logically the same in every
> programming language.
>
> This wish for [foreach] to return a value is really just a wish for a
> logically distinct looping feature which no programming language, not
> just Tcl, seems to support ... or maybe provide a reference, so the
> usefulness claim has some foundation.


The fact that a foreach loop evaluates its body in the current context
doesn't mean it cannot return anything. A foreach call does create a
new callframe for instance (which can be seen by making an error in
the body and examining errorInfo) So it is definitly there.

Mark
Sponsored Links







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

Copyright 2008 codecomments.com