Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It's impossible to recurse a anonymous function in Go without workarounds. #226

Closed
gopherbot opened this issue Nov 16, 2009 · 9 comments
Closed

Comments

@gopherbot
Copy link

by helmwo:

What steps will reproduce the problem?
1.
func main() {
  a := func() {
    ... // how to access this anonymous func here?
  }
  a();
}

What is your $GOOS?  $GOARCH?

linux 386 - but that does not matter.

Which revision are you sync'ed to?  (hg log -l 1)

changeset:   3975:b51fd2d6c160
tag:         tip
user:        Kevin Ballard <kevin@sb.org>
date:        Tue Nov 10 20:05:24 2009 -0800
summary:     Implement new emacs command M-x gofmt

Please provide any additional information below.

I'd introduce a pseudo function called recurse() that allows to recurse 
over functions. I'd even introduce this for named functions.
@gopherbot
Copy link
Author

Comment 1 by jason.catena:

A solution to this should at least consider replacing recursion with iteration for tail 
calls.

@gopherbot
Copy link
Author

Comment 2 by helmwo:

Iteration is nice where it is useful. Same for recursion. Recursion can walk around 
trees, while iteration walks arrays. So there is at least a little bit need for 
recursion in a language...

@gopherbot
Copy link
Author

Comment 3 by consalus:

I believe you can just do:
var a func();
a = func() {...}
and that should work.
At least, a variation of that worked fine for me.

@gopherbot
Copy link
Author

Comment 4 by helmwo:

3: Yes, but imagine you change "a" somewhere.

@griesemer
Copy link
Contributor

Comment 5:

At the moment there is a clear work-around as has been pointed out.

Status changed to Thinking.

@gopherbot
Copy link
Author

Comment 6 by JQBalter:

"Iteration is nice where it is useful. Same for recursion. Recursion can walk around 
trees, while iteration walks arrays. So there is at least a little bit need for 
recursion in a language..."
I believe comment 1 was about implementing recursive tail calls as iteration, not for
eliminating recursion.
"Yes, but imagine you change "a" somewhere."
Seriously?
anonymous_func_for_a := func() {
    ... anonymous_func_for_a(); ...
}
a := anonymous_func_for_a;
b := a;
a = something_else;
b() // works fine

@rsc
Copy link
Contributor

rsc commented Nov 17, 2009

Comment 7:

Owner changed to r...@golang.org.

@gopherbot
Copy link
Author

Comment 8 by isilando:

These work-around-s are far from clean. There oughts to be a keyword for self-
referencing for anonymous functions, in every language which supports them.
But as the word "ought" indicates, this is pretty low in the priority of issues.

@robpike
Copy link
Contributor

robpike commented Nov 21, 2009

Comment 9:

There's a clear workaround.  If there's a real fix, it's just to allow nested function 
declarations.

Status changed to WontFix.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants