gopherdata / gophernotes

The Go kernel for Jupyter notebooks and nteract.
MIT License
3.86k stars 263 forks source link

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

Closed spolischook closed 3 years ago

spolischook commented 3 years ago

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 commented 3 years ago

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.

spolischook commented 3 years ago

Thanks for explanation