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: unclear whether int, bool, string are considered defined types #21785

Closed
garvitjuniwal opened this issue Sep 7, 2017 · 4 comments
Closed
Milestone

Comments

@garvitjuniwal
Copy link

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.

type A int

var a A
var b int
a = b // this is an error

https://play.golang.org/p/bSE4nIzXhF

@myitcv
Copy link
Member

myitcv commented Sep 7, 2017

From https://golang.org/ref/spec#Types:

Named instances of the boolean, numeric, and string types are predeclared

I read this as "defined types are predeclared for the boolean, numeric and string types and are identified by int, int32, bool, string etc"

Hence they are defined types.

But the sentence that follows in the spec confuses things:

Other named types are introduced with type declarations

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"?

@griesemer griesemer self-assigned this Sep 18, 2017
@griesemer griesemer added this to the Go1.10 milestone Sep 18, 2017
@mdempsky
Copy link
Member

Yes, int, etc are defined types.

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.

@griesemer
Copy link
Contributor

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.

@gopherbot
Copy link

Change https://golang.org/cl/64591 mentions this issue: spec: state which predeclated types are defined or alias types (clarification)

@golang golang locked and limited conversation to collaborators Sep 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants