[1] https://github.com/rackspace/gophercloud/blob/master/accepta...
[1] https://github.com/rackspace/gophercloud/blob/master/accepta...
type AccessFn func(gophercloud.AccessProvider)
so now AccessFn describes any function that takes an AccessProvider as its sole parameter, and returns nothing. On this function type, define WithIdentity as a method (edited for brevity): func (fn AccessFn) WithIdentity(allowReauth bool) {
acc, _ := gophercloud.Authenticate(...)
fn(acc)
}
Inside of the function's method, you can invoke the function itself. Then you would define the actual AccessFn as some variable: fn := AccessFn(func(gophercloud.AccessProvider ) {
// ...
})
and then call its WithIdentity method: fn.WithIdentity(ar)
And then, if you were so inclined, you could define an interface over your WithIdentity method, so that in whatever context you're in, anything that has a WithIdentity method will do. This often (but not always) leads to code that's a bit easier to read. Nested callbacks... not always the easiest to wrap your head around.shameless plug: I've written about this in more detail here, if this sort of thing interests you: http://jordanorelli.tumblr.com/post/42369331748/function-typ...
Deeply nested closures are definitely a problem, but I usually solve that by factoring closures out into top-level functions of their own.
That said, one of Gophercloud's goals is to be as idiomatic as possible. Are you available on #go-nuts if I have questions? I'd like to play with this technique some more and get a feel for it before applying it to (and risk breaking) gophercloud's existing API.
The problem is it doesn't produce any gain in clarity for the purposes I'm using it for. I think for the acceptance tests, it's use is fine.
I can see if it were part of the public API, there's an argument that an interface could be used, but even there, I'm not sure it'd aid readability.
Semantically, however, they're of course identical.