Skip to content

spec: new(expr) should allow "untyped bool values" too #76122

@mdempsky

Description

@mdempsky

The Go spec says:

If the argument expr is an expression of type T, or an untyped constant expression whose default type is T, then new(expr) allocates a variable of type T, initializes it to the value of expr, and returns its address, a value of type *T.

But https://go.dev/play/p/2YcYIT0uTTi?v=gotip accepts this code:

func main() {
	var i int
	fmt.Printf("%T\n", new(i == i))
}

Note: The comparison here is an untyped boolean value, but not an untyped constant.

For comparison, variable declarations says:

If a type is present, each variable is given that type. Otherwise, each variable is given the type of the corresponding initialization value in the assignment. If that value is an untyped constant, it is first implicitly converted to its default type; if it is an untyped boolean value, it is first implicitly converted to type bool.

I think similar text is needed to describe the above compiler behavior.

Or the compiler should be made to reject it, but I think that's less desirable.

(Glad to see this feature finally implemented!)

/cc @alandonovan @griesemer @findleyr

Metadata

Metadata

Assignees

Labels

DocumentationIssues describing a change to documentation.

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions