douchan4674 2017-10-17 01:49
浏览 2

他们为什么不同

In Golang spec:

type (
    T0 []string
    T1 []string
)

it says T0 and T1 are different because they are named types with distinct declarations.but there is a rule: Two named types are identical if their type names originate in the same TypeSpec. so why T0 and T1 are different?


EDIT:

in spec it also says:

A type declaration binds an identifier, the type name, to a new type that has the same underlying type as an existing type, and operations defined for the existing type are also defined for the new type. The new type is different from the existing type.

  • 写回答

1条回答 默认 最新

  • doujuxin7392 2017-10-17 04:32
    关注

    Given:

    type (
        T0 []string
        T1 []string
    )
    

    The Go Programming Language Specification

    Version of June 28, 2017

    A type definition creates a new, distinct type with the same underlying type and operations as the given type, and binds an identifier to it.

    TypeDef = identifier Type .
    

    The new type is called a defined type. It is different from any other type, including the type it is created from.

    A defined type is always different from any other type. Otherwise, two types are identical if their underlying type literals are structurally equivalent; that is, they have the same literal structure and corresponding components have identical types.

    T0 and T1 are defined types and are, therefore, different.


    References:

    The Go Programming Language Specification

    Version of June 28, 2017

    Types

    A type determines a set of values together with operations and methods specific to those values. A type may be denoted by a type name, if it has one, or specified using a type literal, which composes a type from existing types.

    Named instances of the boolean, numeric, and string types are predeclared. Other named types are introduced with type declarations.

    Type declarations

    A type declaration binds an identifier, the type name, to a type. Type declarations come in two forms: alias declarations and type definitions.

    TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
    TypeSpec = AliasDecl | TypeDef .
    

    Alias declarations

    An alias declaration binds an identifier to the given type.

    AliasDecl = identifier "=" Type .
    

    Within the scope of the identifier, it serves as an alias for the type.

    Type definitions

    A type definition creates a new, distinct type with the same underlying type and operations as the given type, and binds an identifier to it.

    TypeDef = identifier Type .
    

    The new type is called a defined type. It is different from any other type, including the type it is created from.

    Type identity

    Two types are either identical or different.

    A defined type is always different from any other type. Otherwise, two types are identical if their underlying type literals are structurally equivalent; that is, they have the same literal structure and corresponding components have identical types.

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)