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

RFC: Go 2: user definable index operators #63515

Closed
metux opened this issue Oct 12, 2023 · 3 comments
Closed

RFC: Go 2: user definable index operators #63515

metux opened this issue Oct 12, 2023 · 3 comments
Labels
LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change

Comments

@metux
Copy link

metux commented Oct 12, 2023

This is just an draft for discussion, not a formal proposal yet.

Author background

  • experienced, already touched toolchain code (also packagjng it for various distros)
  • other languages: too many to list'em all ;-)

Related proposals

  • haven't found similar proposal yet
  • no impact on error handling or generics

Precedence in other languages

  • python translates index operator to get() and set() methods
  • any class can implement them and so provide index operator
  • IOW: the [xxx] notation is just syntactic sugar for method calls
  • c++ allows custom operator implementations, which also go down to function/method calls, under the hood

Proposal

  • add support for user defined index operations ([foo]), these are routed to function/interface calls
  • helpful for reducing boilerplate for typical index-like operations
  • by employing interfaces, consumer code can be made agnostic of the actual type its operating on, while still using the compact slice/map-like syntax
  • by implementing corresponding interface on maps or slices themselves, those can be directly used whereever the that interface is demanded

How does it work:

  • when compiler encounters an index access on some custom type (or interface) it looks for a matching getter/setter function (exact naming scheme yet to be defined)
  • the compiler just rewrites it to a direct getter/setter call (ast transformation) and tries to resolve the function call the usual way.
  • so now, instead of foo.Get(bar) one can just write foo[bar], as well as foo[bar] = x instead of foo.Set(x)
  • the getter actually returns the value type as well as an ok flag
  • Yet leaving out iterators, since that's orthogonal to index operator and IMHO should be done independently.

Compatibility:

  • existing code remains uneffected
  • side effect: previously broken (non-compilable) code could become valid (if there happens to be an existing function that looks like a getter/setter by accident), but the practical risks should be quite zero.
  • error messages on broken code (trying index access on type that doesnt support it) might look differently - might complain on missing function now (depending on actual implementation). OTOH, that might be actually more useful than harmful, by giving the dev better hints on what he shall do, if he really wants index access

Costs

  • adds a little bit more complexity to the language itself
  • readers of source, who dont know this feature yet, might wonder how code using it could ever work/compile at all
  • affected tool: only those who actually with compiling (not just formatting etc)
  • tiny increase of compile time for code using that feature: at the point build breaks now, we'll have little ast transformation and continue compiling
  • no runtime overhead
@metux metux added LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change labels Oct 12, 2023
@seankhliao
Copy link
Member

while it doesn't specifically mention indexing, I think operator overloading should be folded into #27605

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Oct 12, 2023
@metux
Copy link
Author

metux commented Oct 12, 2023

while it doesn't specifically mention indexing, I think operator overloading should be folded into #27605

No, this is quite different. The one you've mentioned deals with arithmetic operators. And explicitly demands the operants to be immutable, which totally contradicts the index operator, since changing elements belongs to its sole purpose.

Oh, and closing the ticket (whose sole purpose is starting a discussion) without any discussion is quite the opposite of opensource culture.

I wonder how @ianlancetaylor or @rsc, @robpike have to say about this.

@ianlancetaylor
Copy link
Contributor

We are not going to support overriding the [] operator without support overriding of other operators. All the same problems arise for both kinds of overriding. So I think that de-duping this proposal is correct.

I appreciate that you are looking for a discussion. But we've found that GitHub issues are not a good place for that. They lack threading and if many people comment then comments are hidden by default and difficult to find in practice, leading to constant repetition. The proposal process is really for specific proposals. For discussions, please use a forum like golang-nuts. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

3 participants