From 336ba27ce81ea019693c4a63bc970f706a70c9fc Mon Sep 17 00:00:00 2001 From: Go101 <22589241+go101@users.noreply.github.com> Date: Sun, 26 May 2019 13:00:47 +0800 Subject: [PATCH] Go 1 incompatible --- README-v9.a.md | 35 ++++++++++++++++++++++++++++------- README.md | 2 +- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README-v9.a.md b/README-v9.a.md index e15e823..1858893 100644 --- a/README-v9.a.md +++ b/README-v9.a.md @@ -1,7 +1,7 @@ # A proposal to support read-only and practical immutable values in Go Comparing to the v9.1 revison, this revision (v9.a) -* removes the **final value** concept, thus this branch doesn't need the `final` keyword and becomes Go 1 compatible. +* removes the **final value** concept, thus this branch doesn't need the `final` keyword and becomes ([almost](#go-1-incompatible-cases)) Go 1 compatible. * removes the restriction that sending to and receiving from read-only channels are disallowed. Any criticisms and improvement ideas are welcome, for @@ -54,7 +54,7 @@ Please note, * A reader value, if it is not a read-only value, might be modifiable. * A writer value, if it is not a writable value, might not be modifiable. -Also note, values of some some types never reference any values, +Also note, values of some types never reference any values, or they are referencing some interval values which can't be modified in any ways in user code. Such types are called **no-references types** below. The **reader** and **writer** roles are non-sense for values of no-references types. @@ -81,6 +81,8 @@ Values of no-references types have adaptive roles when they are used as R-values * Values of no-references types are viewed as writer values when they are assigned to writer or writable values. * Values of no-references types are viewed as reader values when they are assigned to reader or read-only values. +(Maybe it is good to call values of no-references types as unroled values.) + #### new syntax set A notation form `:role` is introduced as value suffixes to indicate the roles of some values, where `role` in the form can only be `rr` (reader) or `ro` (read only). @@ -429,6 +431,25 @@ This information is useful when boxing a reader value into an interface. As above has mentioned, the cap field of a reader byte slice should be set to `-1` if its byte elements are immutable. +## Go 1 incompatible cases and solutions + +There are two Go 1 incompatible cases. + +#### reader arguments + +As above mentioned, when a writer or writable value is passed to a reader parameter, +the writer or writable value must be explicitly converted to a reader or read-only value to act as a legal reader argument. This will break much user code which contains calls to functions in standard packages, such as `fmt.Print`, for the prototype of the `fmt.Print` function is expected to change to `func(...interface{}:rr)`. + +Solution 1: To avoid such incompatibilities, the prototype of the `fmt.Print` can be changed to `func(...interface{}::q)` instead. + +Solution 2: Use `go fix` to fix the calls in user code. + +#### reader/read-only package-level variables + +Many exported `error` values declared (with `var` now) in standard packages are expected to be changed as read-only values (declared with `var:ro` later). However, there might be some user code in whcih these `error` values are assigned to some variables. From the rules mentioned above, such assignments will become illegal later. + +Solution: Use `go fix` to fix these assignment in user code. + ## Rationales #### Rationales of Go needs read-only and immutable values @@ -493,16 +514,16 @@ Sometimes, people may need partial read-only for struct values. type Counter struct { n uint64 - // mu will be always writable, + // mu will be always writable, // even if its containing struct - // value is a read-only value. + // value is a read-only value. mu sync.Mutex:writable } func (c *Counter:rr) Value() uint64 { - // ok. c.mu will be modified, - // which is allowed, even if - // *c is a read-only value. + // ok. c.mu will be modified, + // which is allowed, even if + // *c is a read-only value. c.mu.Lock() defer c.mu.Unlock() // ok diff --git a/README.md b/README.md index cd3a1b7..7298a6f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ This project proposes to support read-only and practical immutable values in Go. Versions: -* [the proposal thread](https://github.com/golang/go/issues/32245)([the old one 1]((https://github.com/golang/go/issues/31464)), [the old one 2](https://github.com/golang/go/issues/29422)), and the [golang-dev thread](https://groups.google.com/forum/#!topic/golang-dev/5M9F09S_k0g) +* [the proposal thread](https://github.com/golang/go/issues/32245) ([the old one 1](https://github.com/golang/go/issues/31464), [the old one 2](https://github.com/golang/go/issues/29422)), and the [golang-dev thread](https://groups.google.com/forum/#!topic/golang-dev/5M9F09S_k0g) * [v0: the initial proposal](README-v0.md) * [v1: the `var:N` version](README-v1.md) * [v2: the pure-immutable-value interpretation version](README-v2.md)