What about here, where the call stack associated with g is still live? func g() error { Top: f := func(err error) { returnfrom Top, nil } c := make(chan struct{}) go func() { defer close(c) f(nil) }() <-c }
? On 14 September 2018 at 06:35, Scott Pakin <scott...@pakin.org> wrote: > On Thursday, September 13, 2018 at 4:00:28 PM UTC-6, Jonathan Amsterdam > wrote: >> >> What's the meaning of this code? >> >> func main() { >> f := g() >> f() >> } >> >> func g() func() { >> Top: >> return func() { returnfrom Top, nil} >> } >> > > It means you're an evil programmer. 😜 > > My thinking is that returning from a no-longer-accessible continuation > should simply be a no-op. Stepping through your example, f := g() > assigns func() { returnfrom Top, nil } to f. At this point the call > stack associated with g() is no longer live. f() executes, turning into > returnfrom > Top, nil. The returnfrom causes the inner function to return immediately > and instructs g() to return nil. But because the continuation > corresponding to the call to g that returned the inner function no longer > exists, there's no place to send that nil, and nothing happens. > > I believe this is a simpler and more Go-like semantics than, say, Scheme's > call/cc, which *does* execute the f := g() statement twice: > > (define (main) > (let ((f (g))) > ;(printf "Calling f = ~s~n" f) > (f))) > > (define (g) > (call/cc > (lambda (Top) > (lambda () > (Top '()))))) > > > (Uncomment the printf to see the double execution. Weird, huh?) > > Thanks for the challenge, > — Scott > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.