You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to Conversions (as of 21.06.2018), the following programs should be illegal, but it compiles without errors:
package main
func main() {
_ = []byte("s") // no compile time error
}
Here we have constant value "s" converted to []byte. According to spec, "s" must be Representable by []byte. The only way to explain the absence of error is to suppose that "s" is in the set of values determined by []byte.
The assumption above contradicts with Assignability rules. Consider the following program that fails to compile:
package main
func main() {
var b []byte
b = "s" // compile time error
}
So, "s" is not assignable to []byte. As a consequence, "s" is not representable by []byte and "s" is not in the set of values determined by []byte.
The text was updated successfully, but these errors were encountered:
However, this is what Conversions section says for constant values:
A constant value x can be converted to type T if x is representable by a value of T. As a special case, an integer constant x can be converted to a string type using the same rule as for non-constant x.
As you can see, conversion from integer constant to string is noted as a special case and is governed by the rules for non-constant x. These rules are described in the section that I missed (Conversions to and from a string type).
But that makes me think that the Conversions to and from a string type is for non-constant values. The section itself does not state explicitly what values the rules are applied to. As a result, it is not obvious that I should consult that section when working with constant values.
Moreover, Conversions section contains the following note:
Specific rules apply to (non-constant) conversions between numeric types or to and from a string type. These conversions may change the representation of x and incur a run-time cost. All other conversions only change the type but not the representation of x.
According to Conversions (as of 21.06.2018), the following programs should be illegal, but it compiles without errors:
Here we have constant value
"s"
converted to[]byte
. According to spec,"s"
must be Representable by[]byte
. The only way to explain the absence of error is to suppose that"s"
is in the set of values determined by[]byte
.The assumption above contradicts with Assignability rules. Consider the following program that fails to compile:
So,
"s"
is not assignable to[]byte
. As a consequence,"s"
is not representable by[]byte
and"s"
is not in the set of values determined by[]byte
.The text was updated successfully, but these errors were encountered: