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

method String() not picked up by compiled code #228

Closed
spolischook opened this issue Feb 11, 2021 · 2 comments
Closed

method String() not picked up by compiled code #228

spolischook opened this issue Feb 11, 2021 · 2 comments
Assignees

Comments

@spolischook
Copy link

There is an example of code snippet that have different result of go run and gophernotes:

type Direction int

const (
	North Direction = iota
	East
	South
	West
)

func (d Direction) String() string {
	return [...]string{"North", "East", "South", "West"}[d]
}

func main() {
	fmt.Print(East)
}

gophernotes example:
Selection_412

go run example:
Selection_413

@cosmos72
Copy link
Member

cosmos72 commented Feb 12, 2021

This has the same root cause as #225
i.e. new named types created by the interpreter are emulated,
and their methods - in this case Direction.toString() - are visible only by interpreted code.

It's also equally unfixable, at least until Go standard library provides a mechanism
to create new named types at runtime and attach methods to them.

A partial workaround is to replace fmt.Print(East) with fmt.Println(East.String())

[UPDATE] I have added this limitation to the relevant section in the main README.md.

@cosmos72 cosmos72 self-assigned this Feb 12, 2021
@cosmos72 cosmos72 changed the title Iota print issue method String() not picked up by compiled code Feb 12, 2021
@spolischook
Copy link
Author

Thanks for explanation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants