From 533dfd62919ecb69a39973cea32d06a1cb166687 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 13 May 2009 16:56:00 -0700 Subject: [PATCH] New type compatibility rules: - changed type equality to type compatibility, updated rules - string literals have ideal string type - conversion w/ relaxed type compatibilty DELTA=123 (26 added, 22 deleted, 75 changed) OCL=28763 CL=28780 --- doc/go_spec.html | 154 ++++++++++++++++++++++++----------------------- 1 file changed, 79 insertions(+), 75 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index de463584f2..a609902fe4 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -8,6 +8,7 @@ Open issues: - no mechanism to declare a local type name: type T P.T Todo's: +[ ] new interface rules per rsc (use "method set" terminology) [ ] document illegality of package-external tuple assignments to structs w/ private fields: P.T(1, 2) illegal since same as P.T(a: 1, b: 2) for a T struct { a b int }. @@ -248,7 +249,7 @@ exponent = ( "e" | "E" ) [ "+" | "-" ] decimals . Integer literals represent values of arbitrary precision, or ideal integers. Similarly, floating-point literals represent values of arbitrary precision, or ideal floats. These ideal -numbers have no size or type and cannot overflow. However, +numbers have no size or named type and cannot overflow. However, when (used in an expression) assigned to a variable or typed constant, the destination must be able to represent the assigned value.

@@ -346,7 +347,9 @@ integer literals.

String literals

-String literals represent constant values of type string. +String literals represent ideal string values. Ideal strings don't +have a named type but they are compatible with type string +(§Type identity and compatibility). There are two forms: raw string literals and interpreted string literals.

@@ -514,7 +517,7 @@ uintptr smallest uint type large enough to store the uninterpreted To avoid portability issues all numeric types are distinct except byte, which is an alias for uint8. Conversions -are required when different numeric types are mixed in an expression +are required when incompatible numeric types are mixed in an expression or assignment. For instance, int32 and int are not the same type even though they may have the same size on a particular architecture. @@ -530,7 +533,7 @@ and false.

Strings

-The string type represents the set of textual string values. +The string type represents the set of string values. Strings behave like arrays of bytes but are immutable: once created, it is impossible to change the contents of a string. @@ -1037,63 +1040,59 @@ received, closed(c) returns true.

General properties of types and values

-Types may be different, structurally equal (or just equal), -or identical. -Go is type safe: different types cannot be mixed -in binary operations and values cannot be assigned to variables of different -types. Values can be assigned to variables of equal type. -

+Two types may be identical, compatible, or incompatible. +Two identical types are always compatible, but two compatible types may not be identical. +Go is type safe: a value of one type cannot be assigned to a variable of an +incompatible type, and two values of incompatible types cannot be mixed in +binary operations.

+ +

Type identity and compatibility

-

Type equality and identity

+

Type identity

-Two type names denote equal types if the types in the corresponding declarations -are equal (§Declarations and Scope). -Two type literals specify equal types if they have the same -literal structure and corresponding components have equal types. -In detail: +Two named types are identical if their type names originate in the same +type declaration (§Declarations and Scope). A named and an unnamed type +are never identical. Two unnamed types are identical if the corresponding +type literals have the same literal structure and corresponding components have +identical types. In detail:

+

Type compatibility

+

-Type identity is more stringent than type equality. -It requires for type names -that they originate in the same type declaration, while for equality it requires -only that they originate in equal type declarations. -Also, the names of parameters and results must match for function types. -In all other respects, the definition of type identity is the -same as for type equality listed above but with ``identical'' -substitued for ``equal''. -

-

-By definition, identical types are also equal types. -Two types are different if they are not equal. +Type compatibility is less stringent than type identity: a named and an unnamed +type are compatible if the respective type literals are compatible. +In all other respects, the definition of type compatibility is the +same as for type identity listed above but with ``compatible'' +substituted for ``identical''.

@@ -1112,50 +1111,50 @@ type (

-these types are equal: +these types are identical:

 T0 and T0
-T0 and T1
-T0 and []string
-T4 and T5
-T3 and struct { a int; c int }
+[]int and []int
+struct { a, b *T5 } and struct { a, b *T5 }
+func (x int, y float) *[]string and func (int, float) (result *[]string)
 

-T2 and T3 are not equal because -they have different field names. +T0 and T1 are neither identical nor compatible +because they are named types with distinct declarations.

-These types are identical: +These types are compatible:

 T0 and T0
-[]int and []int
-struct { a, b *T5 } and struct { a, b *T5 }
+T0 and []string
+T3 and struct { a int; c int }
+T4 and func (x int, y float) *[]string
 

-T0 and T1 are equal but not -identical because they have distinct declarations. +T2 and struct { a, c int } are incompatible because +they have different field names.

Assignment compatibility

Values of any type may always be assigned to variables -of equal static type. Some types and values have conditions under which they may -be assigned to different types: +of compatible static type. Some types and values have conditions under which they may +be assigned to otherwise incompatible types:

Comparison compatibility

-Values of any type may be compared to other values of equal static +Values of any type may be compared to other values of compatible static type. Values of numeric and string type may be compared using the full range of comparison operators as described in §Comparison operators; booleans may be compared only for equality or inequality. @@ -1215,7 +1214,7 @@ Channel and map values are equal if they were created by the same call to (§Making slices, maps, and channels).

  • -Interface values may be compared if they have the same static type. +Interface values may be compared if they have compatible static types. They will be equal only if they have the same dynamic type and the underlying values are equal.
  • @@ -2099,7 +2098,7 @@ For a of type M or *M where M is a map type (§Map types):