proposal: spec: interface literals
Filing this for completeness sake, since it was mentioned in https://github.com/golang/go/issues/21670 that this proposal had been discussed privately prior to Go 1. Just like literals allow the construction of slice, struct, and map values, I propose "interface literals" which specifically construct values that satisfy an interface. The syntax would mirror that of struct literals, where field names would correspond to method names. The original idea is proposed by @Sajmani in https://github.com/golang/go/issues/21670#issuecomment-325739411. Conceptually, this proposal would transform the following initializer ```Go f := func(p int) { /* implementation */ } x := interface{ Name(p int) }{f} ``` to the following type declaration and struct initializer ```Go type _impl_Name_int struct { f func(p int) } func(v _impl_Name_int) Name(p int) { v.f(p) } // … in some other scope f := func(p int) { /* implementation */ } var x interface{ Name(p int) } = _impl_Name_int{f} ``` As an extension to what’s mentioned in #21670, I propose that fields be addressable by both method names and field names, like in this example: ```Go type ReadWriteSeekCloser interface { ReadWriteSeeker Closer } f := os.Open("file") calls := 0 return ReadWriteSeekCloser{ ReadWriteSeeker: f, Close: func() error { if calls < 1 { return f.Close() } return nil }, } ``` The default value for a method is `nil`. Calling a nil method will cause a panic. As a corollary, the interface can be made smaller t
Posted by smasher164 on Jun 13, 2018, 6:14 AM
Tags
Related issues
- [Meta] Feedback on granular control over Copilot changes
- unexpected status 401 Unauthorized issue
- [MODEL] Measurable quality regression in Claude generation 5 (Fable 5 / Opus 5 / Sonnet 5): worse nonsense detection, ~2x verbosity, under-disclosed model fallb
- [CRITICAL] Subagent spawning and subagent pattern bugs trigger infinite recursion, infinite token usage, grossly inefficient token usage, and lost accumulated s
- Video stops playing after some minutes
- [MODEL] Claude verbose code comments by default — ignores instructions to stop.
Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments
github (Software)
[Meta] Feedback on granular control over Copilot changes
unexpected status 401 Unauthorized issue
[MODEL] Measurable quality regression in Claude generation 5 (Fable 5 / Opus 5 / Sonnet 5): worse nonsense detection, ~2x verbosity, under-disclosed model fallb
[CRITICAL] Subagent spawning and subagent pattern bugs trigger infinite recursion, infinite token usage, grossly inefficient token usage, and lost accumulated s
Video stops playing after some minutes
[MODEL] Claude verbose code comments by default — ignores instructions to stop.
Scope Codex VS Code chats to the current workspace/project
Update on "Co-authored-by: Copilot" in commit messages
proposal: spec: interface literals
Filing this for completeness sake, since it was mentioned in https://github.com/golang/go/issues/21670 that this proposal had been discussed privately prior to Go 1. Just like literals allow the construction of slice, struct, and map values, I propose "interface literals" which specifically construct values that satisfy an interface. The syntax would mirror that of struct literals, where field names would correspond to method names. The original idea is proposed by @Sajmani in https://github.com/golang/go/issues/21670#issuecomment-325739411. Conceptually, this proposal would transform the following initializer ```Go f := func(p int) { /* implementation */ } x := interface{ Name(p int) }{f} ``` to the following type declaration and struct initializer ```Go type _impl_Name_int struct { f func(p int) } func(v _impl_Name_int) Name(p int) { v.f(p) } // … in some other scope f := func(p int) { /* implementation */ } var x interface{ Name(p int) } = _impl_Name_int{f} ``` As an extension to what’s mentioned in #21670, I propose that fields be addressable by both method names and field names, like in this example: ```Go type ReadWriteSeekCloser interface { ReadWriteSeeker Closer } f := os.Open("file") calls := 0 return ReadWriteSeekCloser{ ReadWriteSeeker: f, Close: func() error { if calls < 1 { return f.Close() } return nil }, } ``` The default value for a method is `nil`. Calling a nil method will cause a panic. As a corollary, the interface can be made smaller t
Comments (0)
No comments yet. Start the discussion.