IssueRaiser

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

Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments

github (Software)

proposal: spec: interface literals

smasher164Jun 13, 2018, 6:14 AM

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.

Comment on this issue