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: unclear whether int, bool, string are considered defined types #21785
Comments
From https://golang.org/ref/spec#Types:
I read this as "defined types are predeclared for the boolean, numeric and string types and are identified by Hence they are defined types. But the sentence that follows in the spec confuses things:
It is, I think, a hangover from pre-alias days, as was picked up in this comment @griesemer @mdempsky - should that reference to "named type" now be "defined type"? |
Yes, As @myitcv points out, we used to just say "named" type. Now that there are type aliases (which have names but may refer to an underlying anonymous type literal), we instead say "defined" type to refer to named types that aren't aliases. |
We still have the notion of a "named type": Like before (the split into defined and alias types) it's a type that has a name. Unlike before, having a name alone doesn't give it special meaning; we also have to look at its declaration. We need the notion of a named type in other places, such as where a type name is needed. The predeclared types are named types (they have a name), and most of them (expect for byte and rune) are defined types. It's true that they are somewhat special insofar as their underlying types are themselves. We could call the predefined types but that would require yet another term to be introduced. Instead, I opted for simply stating explicitly which predeclared types are defined types and which ones are aliases. CL forthcoming. |
Change https://golang.org/cl/64591 mentions this issue: |
As per the spec, one of the cases in which a value x is assignable to a variable of type T ("x is assignable to T") is
x's type V and T have identical underlying types and at least one of V or T is not a defined type.
Again, from the spec,
A type definition creates a new, distinct type with the same underlying type and operations as the given type, and binds an identifier to it.
The new type is called a defined type. It is different from any other type, including the type it is created from.
Thus by definition int, string, bool are not defined types. But it is still an error to assign an int to another defined type with the same underlying type int.
https://play.golang.org/p/bSE4nIzXhF
The text was updated successfully, but these errors were encountered: