英文:
What "a constant index that is untyped is given type int" means?
问题
Go规范给出了索引表达式a[x]
的规则:
如果
a
不是一个映射:
- 索引
x
必须是整数类型或无类型常量- 常量索引必须是非负的,并且可以用int类型的值表示
- 无类型的常量索引被赋予int类型
- 如果0 <= x < len(a),则索引
x
在范围内,否则超出范围
第三条规则(无类型的常量索引被赋予int类型)的确切含义是什么?难道一个有效的表达式a[1.0]
不违反这条规则吗?
英文:
Go specification gives the rules for index expression a[x]
:
> If a
is not a map:
>
> - the index x must be of integer type or an untyped constant
> - a constant index must be non-negative and representable by a value of type int
> - a constant index that is untyped is given type int
> - the index x is in range if 0 <= x < len(a), otherwise it is out of range
What does exactly the third rule (a constant index that is untyped is given type int ) mean? Doesn't a valid expression a[1.0]
contradict that rule?
答案1
得分: 2
在运行时,每个值都必须具有类型。如果使用未指定类型的常量,它将被转换为具有类型的值,例如:
fmt.Printf("%T %v", 1, 1)
1
是一个未指定类型的常量,但是当传递给 fmt.Printf()
时,它必须被转换为具有类型的值。在上面的示例中,它的默认类型将被使用,即 int
,所以上面的示例输出 int 1
。
- 未指定类型的常量索引将被赋予
int
类型
这意味着如果您在索引中使用了一个未指定类型的常量值(它没有类型),那么在将常量转换为具有类型的值时将使用 int
。
例如:
s := []int{1, 2, 3}
fmt.Println(s[1])
这里使用了未指定类型的常量 1
作为索引,因此它将被转换/使用为 int
。
一个反例:
fmt.Println(s[int32(1)])
这里使用了具有类型的值 int32(1)
作为索引。这是有效的,因为 int32
是一个整数类型。
该规则只是说明如果使用了未指定类型的常量,那么它将被赋予 int
类型,而不是例如 int32
。
当使用整数字面值时,这可能显而易见,但是当使用其他类型的字面值时,例如符文字面值,就不那么明显了。
以下示例也是有效的:
fmt.Println(s['\x00'])
上面的示例使用了符文字面值 '\x00'
作为索引,它也是一个未指定类型的常量,但它具有不同的默认类型。符文字面值的默认类型是 rune
,它是 int32
的别名。引用的索引规则指出,在这种情况下,索引将是 int
类型,而不是 int32
。
英文:
Every value used at runtime must have a type. If you use an untyped constant, it will be converted to a typed value such as in this example:
fmt.Printf("%T %v", 1, 1)
1
is an untyped constant, but when passed to fmt.Printf()
, it must be converted to a typed value. In the example above, its default type will be used which is int
, so the above example prints int 1
.
> - a constant index that is untyped is given type int
This means that if you use a constant value for the index that is untyped (it has no type), then int
will be used when the constant is converted to a typed value.
For example:
s := []int{1, 2, 3}
fmt.Println(s[1])
Here the untyped constant 1
is used as the index, so it will be converted / used as int
.
A counter-example:
fmt.Println(s[int32(1)])
Here s
is indexed with a typed value int32(1)
. This is valid because int32
is an integer type.
The rule just says that if you use an untyped constant, then it will be given int
type, and not int32
for example.
This may seem obvious when using integer literals, but not when using other kind of literals, such as rune literals.
The following is also valid:
fmt.Println(s['\x00'])
The above example uses a rune literal '\x00'
as the index, which is also an untyped constant but it has a different default type. Rune literals have default type rune
which is an alias for int32
. The quoted indexing rule states that in this case the index will be of type int
and not int32
.
答案2
得分: 0
这不会发生。1.0
和1
是相同的数字(在数学上),所以未指定类型的常量1.0
可以被赋予int
类型。
然而,如果你尝试指定a[1.5]
,你会得到一个错误,因为1.5
不是整数。
英文:
It doesn't. 1.0
and 1
are the same number (mathematically), so the untyped constant 1.0
can be given a type int
.
If you'll, however, try to specify a[1.5]
, you'll get an error, since 1.5
is not an integer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论