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

spec: embedded interfaces should include interface type literals #69481

Closed
TutiFrouti opened this issue Sep 15, 2024 · 3 comments
Closed

spec: embedded interfaces should include interface type literals #69481

TutiFrouti opened this issue Sep 15, 2024 · 3 comments
Assignees
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@TutiFrouti
Copy link

What is the URL of the page with the issue?

https://go.dev/ref/spec#Embedded_interfaces

What is your user agent?

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36

Screenshot

No response

What did you do?

Specification for embedded interfaces states (I bolded myself):

In a slightly more general form an interface T may use a (possibly qualified) interface type name E as an interface element. This is called embedding interface E in T [Go 1.14].

It requires interface type name, but implementation currently accepts interface type literal as well.

The notes for GO 1.18 at the end of the specification also imply that it was only intended for interface type names, not interface type literals (I bolded myself):

Interface types may embed arbitrary types (not just type names of interfaces) as well as union and ~T type elements.

What did you see happen?

type InterfaceX interface {
	interface {
		Print1()
		Print2()
	}
}

It compiles successfully.

What did you expect to see?

I expect the implementation to remain as it is currently and just to update the specification.

That is, to change the following:

In a slightly more general form an interface T may use a (possibly qualified) interface type name E as an interface element.

to:

In a slightly more general form an interface T may use a interface type (name or literal) E as an interface element.

or:

In a slightly more general form an interface T may use a interface type E as an interface element.

It doesn't need to be an interface type name, just interface type.

@TutiFrouti TutiFrouti changed the title spec doesn't correctly address embedding interfaces (interface type literals can be embedded as well) spec doesn't correctly address embedded interfaces (interface type literals can be embedded as well) Sep 15, 2024
@seankhliao seankhliao changed the title spec doesn't correctly address embedded interfaces (interface type literals can be embedded as well) spec: embedded interfaces should include interface type literals Sep 15, 2024
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 15, 2024
@ianlancetaylor
Copy link
Contributor

CC @griesemer

@griesemer griesemer self-assigned this Sep 16, 2024
@griesemer griesemer added this to the Go1.24 milestone Sep 16, 2024
@griesemer
Copy link
Contributor

Thanks for raising the issue, but the spec is correct as is. As you cite yourself, the spec says:

In a slightly more general form an interface T may use a (possibly qualified) interface type name E as an interface element.

The emphasis here is on the word may. It doesn't say that that's the only choice. In fact, the section on general interfaces allows the "embedding" of interface literals (and other types).

The reason that the spec groups interfaces into 3 categories (basic interfaces, interfaces with embedding, general interfaces) is purely for didactic and historical reasons. Before Go 1.18, we just had basic interfaces and embedding of other interfaces. This is covered by the first two sub-sections on interface types. Then, with Go 1.18, interfaces were generalized and now represent type sets (rather than just method sets). In the spec, we could probably just describe general interfaces, but it would be a) harder to grasp the concept, and b) remove the connection to pre-Go 1.18 interfaces. It may be fine for a spec, but the current organization was deliberate to help bringing people along. Also, many Go programmers never need the most general interface definition.

In short, "embedding" an interface literal is clearly permitted by the spec even though we do not traditionally call that an embedded interface.

Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants