How the hack type comparable interface{ comparable } compiles

I see an interface structure in the golang 1.18 codebase in builtin.go

// comparable is an interface that is implemented by all comparable types
// (booleans, numbers, strings, pointers, channels, arrays of comparable types,
// structs whose fields are all comparable types).
// The comparable interface may only be used as a type parameter constraint,
// not as the type of a variable.
type comparable interface{ comparable }

I can’t understand how this can compile. If I copy this in my main.go the compiler says recursive interface definition.

// That compiles 
// But What I can I do with that ? 
type myInterface interface{ string }

}
// That compiles  too 
type MyStruct struct {
	A string
	B bool
}

type MyStruct2 struct {
	B string
	C bool
}

type myInterface interface {
	MyStruct
	MyStruct2
	Foo()
}

type MyStruct3 struct {

}


func (o MyStruct3) Foo() {
// So what ca I do with MyStruct and MyStruct2 at this moment ? 
}

The builtin package’s documentation says this:

Package builtin provides documentation for Go’s predeclared identifiers. The items documented here are not actually in package builtin but their descriptions here allow godoc to present documentation for the language’s special identifiers.

So the definitions don’t make a whole lot of sense because the names in the package are “built in.” Other data types like string and int seem to be defined in terms of themselves, for example.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.