In the following code:
type boolValue bool
func newBoolValue(val bool, p *bool) *boolValue {
*p = val
return (*boolValue)(p)
}
What is the last line doing?
In the following code:
type boolValue bool
func newBoolValue(val bool, p *bool) *boolValue {
*p = val
return (*boolValue)(p)
}
What is the last line doing?
The Go Programming Language Specification
Conversions are expressions of the form
T(x
) whereT
is a type andx
is an expression that can be converted to typeT
.
type boolValue bool
func newBoolValue(val bool, p *bool) *boolValue {
*p = val
return (*boolValue)(p)
}
(*boolValue)(p)
is doing a type conversion from *bool
, the type of p
, to *boolValue
, the type of the newBoolValue
function return value. Go requires explicit conversions. The conversion is allowed because bool
is the underlying type of boolValue
.
If you simply write return p
without the conversion the compiler error message explains the problem:
return p
error: cannot use p (type *bool) as type *boolValue in return argument