0

Normalization of method selectors

 2 years ago
source link: https://go101.org/details-and-tips/method-selector-normalization.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Normalization of method selectors

Go allows simplified forms of some selectors.

For example, in the following program, t1.M1 is a simplified form of (*t1).M1, and t2.M2 is a simplified form of (&t2).M2. At compile time, the compiler will normalize the simplified forms to their original respective full forms.

The following program prints 0 and 9, because the modification to t1.X has no effects on the evaluation result of *t1 during evaluating (*t1).M1.

package main

type T struct {
	X int
}

func (t T) M1() int {
	return t.X
}

func (t *T) M2() int {
	return t.X
}

func main() {
	var t1 = new(T)
	var f1 = t1.M1 // <=> (*t1).M1
	t1.X = 9
	println(f1()) // 0 
	
	var t2 T
	var f2 = t2.M2 // <=> (&t2).M2
	t2.X = 9
	println(f2()) // 9
}

In the following code, the function foo runs okay, but the function bar will produce a panic. The reason is s.M is a simplified form of (*s.T).M. At compile time, the compiler will normalize the simplified form to it original full form. At runtime, if s.T is nil, then the evaluation of *s.T will cause a panic. The two modifications to s.T have no effects on the evaluation result of *s.T.

package main

type T struct {
	X int
}

func (t T) M() int {
	return t.X
}

type S struct {
	*T
}

func foo() {
	var s = S{T: new(T)}
	var f = s.M // <=> (*s.T).M
	s.T = nil
	f()
}

func bar() {
	var s S
	var f = s.M // panic
	s.T = new(T)
	f()
}

func main() {
	foo()
	bar()
}

Please note that, interface method values and method values got through reflection will be expanded to the promoted method values with a delay. For example, in the following program, the modification to s.T.X has effects on the return results of the method values got through reflection and interface ways.

package main

import "reflect"

type T struct {
	X int
}

func (t T) M() int {
	return t.X
}

type S struct {
	*T
}

func main() {
	var s = S{T: new(T)}
	var f = s.M // <=> (*s.T).M
	var g = reflect.ValueOf(&s).Elem().
		MethodByName("M").
		Interface().(func() int)
	var h = interface{M() int}(s).M
	s.T.X = 3
	println( f() ) // 0
	println( g() ) // 3
	println( h() ) // 3
}

Source: https://github.com/golang/go/issues/47863

However, as of Go toolchain 1.18, there is a bug in the official standard Go compiler implementation. The compiler will de-virtualize some interface methods at compile time but the de-virtualizations are made too far to be correct. For example, the following program should print 2 2, but it prints 1 2.

package main

type I interface{ M() }

type T struct{
	x int
}

func (t T) M() {
	println(t.x)
}

func main() {
	var t = &T{x: 1}
	var i I = t
	
	var f = i.M
	defer f() // 2 (correct)

	// i.M is de-virtualized as (*t).M at compile time (bug).
	defer i.M() // 1 (wrong)
	
	t.x = 2
}

It is unclear when this bug will be fixed now.

Source: https://github.com/golang/go/issues/52072



The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.

If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @go100and1 or join Go 101 slack channels.

The digital versions of this book are available at the following places:

Tapir, the author of Go 101, has been on writing the Go 101 series books and maintaining the go101.org website since 2016 July. New contents will be continually added to the book and the website from time to time. Tapir is also an indie game developer. You can also support Go 101 by playing Tapir's games (made for both Android and iPhone/iPad):

Individual donations via PayPal are also welcome.


Index:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK