reject_method_value.mx raw

   1  // expected: closure captures outer variable(s) [recv]
   2  //
   3  // A bound method value holds its receiver, which is a capture: the closure
   4  // would have to carry the operand. Legacy rejects it for that reason, so
   5  // `defer c.Close()` is the only way to defer a method call - there the
   6  // receiver travels as an argument on the defer record, not in an environment.
   7  package main
   8  
   9  type Counter struct {
  10  	n int32
  11  }
  12  
  13  func (c *Counter) Close() {
  14  	c.n = c.n + 1
  15  }
  16  
  17  func main() {
  18  	c := &Counter{}
  19  	f := c.Close
  20  	f()
  21  }
  22