Coder Social home page Coder Social logo

Comments (3)

rabbbit avatar rabbbit commented on August 15, 2024 1

This seems to be expected given how defer works in Go.

https://go.dev/play/p/eF8TmyEnTxt

func t1() int {
	var i int
	defer func() {
		i++
	}()
	return i
}

func t2() (i int) {
	defer func() {
		i++
	}()
	return i
}

func main() {
	fmt.Println(t1())
	fmt.Println(t2())
}

Results in:

0
1
Program exited.

Even playing with pointers does not work around this:

func t3() int {
	var i int
	ii := &i
	defer func() {
		*ii++
	}()
	return *ii
}
// returns 0

My brief reading of the spec here https://go.dev/ref/spec#Defer_statements calls out that you need to be using named returns for defers to modify the return value:

For instance, if the deferred function is a function literal and the surrounding function has named result parameters that are in scope within the literal, the deferred function may access and modify the result parameters before they are returned.

from multierr.

abhinav avatar abhinav commented on August 15, 2024 1

This is a good point, but yeah @rabbbit is right, because of how defers work, the library can't address this.
We can do two things here:

  • Update the documentation of AppendInvoke to loudly state that if you're using it inside a defer, the variable must be a named return.
  • In the future, we can look into building a go/analysis-based linter to catch these cases since this is pretty easily lintable.

from multierr.

abhinav avatar abhinav commented on August 15, 2024 1

Closing because documentation was updated in #63, and there's nothing we can do at the library level besides that.

from multierr.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.