“给定类型 int 的无类型常量索引”是什么意思?

huangapple go评论87阅读模式
英文:

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(&quot;%T %v&quot;, 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[&#39;\x00&#39;])

The above example uses a rune literal &#39;\x00&#39; 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.01是相同的数字(在数学上),所以未指定类型的常量1.0可以被赋予int类型。

然而,如果你尝试指定a[1.5],你会得到一个错误,因为1.5不是整数。

playground

英文:

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.

playground

huangapple
  • 本文由 发表于 2021年10月11日 18:58:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/69525096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定