google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.31k stars 209 forks source link

Allow overriding `repr` for custom types #535

Open oprypin opened 6 months ago

oprypin commented 6 months ago

In Bazel's Starlark, the following holds:

However in starlark-go it is impossible to implement a Label object that would behave like this, instead the value of repr is forced to be '"//:hi"' per the straightforward implementation:

https://github.com/google/starlark-go/blob/e6e8e7ce1b7a8dff097a7e16bdacc53590e942ae/starlark/library.go#L958

Are there any plans to allow overriding repr of a particular type to be able to achieve this distinction?

adonovan commented 6 months ago

Are there any plans to allow overriding repr of a particular type to be able to achieve this distinction?

No, there are no plans. If this feature is really important it could be supported by defining a Reprable interface that the repr function would probe for, but our experience building a Bazel-like tool using starlark-go was that this wasn't necessary, even though it was technically a point of difference between the Go API and the java.starlark.net API.