This proposal not Go 1 compatible. Please read the last section of this proposal for incompatible cases.
Any criticisms and improvement ideas are welcome, for
- I have not much compiler-related knowledge, so the following designs may have flaws.
- I haven't found a perfect syntax notation set for this proposal yet.
The problems this proposal tries to solve:
- no ways to declare package-level immutable non-basic values.
- no ways to declare immutable function parameters and results.
Please note, the immutability semantics in this proposal is different
from either the const values in C/C++ or in JavaScript.
The following sections will explain the differences.
We know each value has a property, self_modifiable, which means whether or not that value is modifiable.
This proposal will add a new value property ref_modifiable for each value, which means
whether or not the values referenced (either directly or indirectly) by that value are modifiable.
The permutation of thw two properties result 4 genres of values:
{self_modifiable: true, ref_modifiable: true}. Such as variables.{self_modifiable: true, ref_modifiable: false}. No such Go values currently.{self_modifiable: false, ref_modifiable: true}. Such as composite literals. (In fact, all declared constants in JavaScript and all final variables decalred in Java belong to this genre.){self_modifiable: false, ref_modifiable: false}. No such Go values currently.
(Note, in fact, we can catagory declared function values and constant basic values into either the 3rd or the 4th genre.)
This proposal treats the self_modifiable as a direct value property,
and treats ref_modifiable as a type property (an indirect value property).
This proposal will let Go support the two value genres the current Go doesn't support,
and extend the range of {self_modifiable: false, ref_modifiable: true} values.
{self_modifiable: true}values are declared withvar.{self_modifiable: false}values are declared withfinal(a new keyword). Please note that, although afinalvalue itself can't be modified, the values referenced by thefinalvalue might be modifiable. (This is the same as JavaScriptconstvalues and Javafinalvalues.)
Types with property {ref_modifiable: false} are called immutable types.
The notation T.fixed is introduced to represent the immutable version of mutable type T,
where fixed is a new introduced keyword.
Please note the semantics of immutable type in this proposal is different from many other immutable type proposals.
A value of type T.fixed may be modifiable, it is just that the values referenced (either directly or indirectly)
by the T.fixed value can't be modified.
Below, the proposal will call
Tvalues declared withvarasvar.mutablevalues.Tvalues declared withfinalasfinal.mutablevalues.T.fixedvalues declared withvarasvar.fixedvalues.T.fixedvalues declared withfinalasfinal.fixedvalues.
Please note that,
- the notation
[]*chan T.fixedcan only mean([]*chan T).fixed, whereas[]*chan (T.fixed),[]*((chan T).fixed)and[]((*chan T).fixed)are all invalid notations. fixedis not allowed to appear in type declarations.type T []int.fixedis invalid.- the respective immutable types of no-reference types (including basic types, struct types with only fields of no-reference types and array types with no-reference element types) are the mutable types themselves.
A notation v.(fixed) is introduced to convert a value v to a *.fixed value.
The notation is called immutability assertion.
If v is a non-interface values, v.(fixed) will always succeed.
This notation is mainly used in two situations:
- assert a
*.mutableinterface value to a*.fixedinterface value. - use
v.(fixed)as the initial values for new declared values so that compilers can deduce the new declared values are*.fixedvalues.
The basic assignment/binding rules:
*.mutablevalues can be bound/assigned to a*.mutablevalue.- A
final.*value must be bound a value in its declaration. After the declaration, it can never be assigned any more. - Generally, any value can be bound/assigned to a
*.fixedvalue, including constants, literals, variables, and the new supported values by this proposal, with one exception:*.mutableinterface values can't be assigned to*.fixedinterface values. A*.mutableinterface value can only be immutability asserted to a*.fixedinterface value. (Please view the interface related rules section below for details.) - Generally,
*.fixedvalues can't be bound/assigned to a*.mutablevalue, with one exception:*.fixedvalues of no-reference types will be viewed as be viewed as*.mutablevalues when they are used as source values in assignments. (Maybe function types should be also viewed as no-reference types.)
Please note that, although a value can't be modified through *.fixed values which are referencing it, it
might be modified through other *.mutable values which are referencing it. (Yes, this proposal doesn't solve all problems.)
In other words, most of the rules in this proposal are enfored by compilers, not runtimes.
The section to the next will list the detailed rules for values of all kinds of types. Those rules are much straightforward and anticipated. They are derived from the above mentioned basic assignment/binding rules.
Please note, the immutability semantics in this proposal is different from the const semantics in C/C++.
For example, a value declared as var p ***int.fixed in this proposal is
like a variable decalared as int const * const * const * p in C/C++.
In C/C++, we can declare a variable as int * const * const * x,
but there are no ways to declare variables with the similar immutabilities in this proposal.
(In other words, this proposal assumes such use cases are rare in practice.)
Another example, the following C code are valid.
#include <stdio.h>
typedef struct T {
int* y;
} T;
void main() {
int a = 123;
T t = {.y = &a};
const T* p = &t; // <=> T const * p = &t;
*p->y = 789; // allowed
printf("%d\n", *t.y); // 789
}But, the following similar Go code is invalid by this proposal.
package main
type T struct{
y *int
}
func main() {
var a int = 123
var t = T{y: &a}
var p *T.fixed = &t; // a value with property:
// {self_modifiable: true, ref_modifiable: false}
*p.y = 789; // NOT allowed,
// for all values referenced by p, either
// directly or indirectly, are unmodifiable.
println(*t.y);
}It is a challenge to design a both simple and readable syntax set for this proposal. The current design may be not perfect, so any improvemnt ideas are welcome.
Some examples of the full value declaration form:
final FileNotExist = errors.New("file not exist").(fixed) // a totally immutable value
final FileNotExist .fixed = errors.New("file not exist") // equivalent to the above line
// The following declarations are equivalent.
var a []int.fixed
var a .fixed = []int{1, 2, 3}
var a = []int{1, 2, 3}.(fixed)
// The following declarations are equivalent (for no-reference types only).
var b int
var b int.fixed
// Declare variables in a hybrid way.
// x is a var.fixed value, y is a var.mutable value.
var x, y = []int{1, 2}.(fixed), []int{1, 2}
// z is a final.mutable value, w is a final.fixed value.
final z, w []int = []int{1, 2}, []int{1, 2}.(fixed)Immutable parameter and result declaration examples:
func Foo(m http.Request.fixed, n map[string]int.fixed) (o []int.fixed, p chan int.fixed) {...}
func Print(values ...interface{}.fixed) {...}All parameters and results in the above example are var.fixed values.
To avoid syntax design complexity, final.* parameters and results are not supported.
Short value declaration examples:
{
oldA, newB := va, vb.(fixed)
oldA, newB := va, (.fixed)(vb) // equivalent to the above line
newX, oldY := (Tx.fixed)(va), vy
newX, oldY := (Tx(va)).(fixed), vy
newX, oldY := Tx(va.(fixed)), vy
newX, oldY := Tx(va).(fixed), vy // equivalent to the above three lines
}Again, to avoid syntax design complexity, final.* values can't be declared in short declartions.
In other words, values declared in short declarations are always var.* values.
- Dereferences of
*.fixedpointers arefinal.fixedvalues. - Dereferences of
*.mutablepointers arevar.mutablevalues. - Addresses of addressable
final.*and*.fixedvalues arevar.fixedpointer values. Some certain write permissions are lost when taking addresses of addressablefinal.mutableandvar.fixedvalues.
Yes, final.* values may be addressable.
- Dereferences of an unsafe pointer are always
var.mutablevalues, even if the unsafe pointer is a*.fixedvalue. (This is important for refection implementation.)
- Fields of
var.fixedstruct values arevar.fixedvalues. - Fields of
final.fixedstruct values arefinal.fixedvalues. - Fields of
final.mutablestruct values arefinal.mutablevalues.
- Elements of
var.fixedarray values arevar.fixedvalues. - Elements of
final.fixedarray values arefinal.fixedvalues. - Elements of
final.mutablearray values arefinal.mutablevalues.
- Elements of
*.fixedslice values arefinal.fixedvalues. - Elements of
*.mutableslice values arevar.mutablevalues. - We can't append elements to
final.*and*.fixedslice values. - Subslice:
- The subslice result of a
final.fixedslice is still afinal.fixedslice. - The subslice result of a
final.mutableslice is still afinal.mutableslice. - The subslice result of a
var.fixedslice is still avar.fixedslice.
- The subslice result of a
- Elements of
*.fixedmap values arefinal.fixedvalues. - Elements of
*.mutablemap values arevar.mutablevalues. - We can't append new entries to (or replace entries of,
or delete old entries from)
*.fixedmap values.
The method set of type T.fixed is a subset of type T.
If T is an interface type, then the method sets of T.fixed and T are always identical.
For type T and *T, if methods can be declared for them (either explicitly or implicitly),
the method set of type T.fixed is a subset of type *T.fixed.
(Or in other words, the method set of type T is a subset of type *T
if type T is not an interface type.)
- Dynamic type
- The dynamic type of a
*.mutableinterface value is a mutable type. - The dynamic type of a
*.fixedinterface value is an immutable type.
- The dynamic type of a
- Box
- No values can be boxed into
final.*interface values (except the initial bound values). *.fixedvalues can't be boxed intovar.mutableinterface values.- Any value can be boxed into a
var.fixedinterface value (as long as the method set ofT.fixedimplements the type of the interface value, whereTis the corresponding mutable type of the value to be boxed).
- No values can be boxed into
- Assert
- A type assertion on a
*.fixedinterface value results a*.fixedvalue. (It is not important whether or not the result itself can be modified.) For such an assertion, its syntax formx.(T.fixed)can be simplified asx.(T). - A type assertion on a
*.mutableinterface value results a*.mutablevalue. (It is not important whether or not the result itself can be modified.) - An immutability assertion on a
*.mutableinterface value results a*.fixedvalue. (It is not important whether or not the result itself can be modified.) Such an assertion fails if the immutable version of the dynamic type of the interface value doesn't implement the type of the interface value. Same as type assertions, an immutability assertion may return an optional second untyped bool which indicates whether the assetsion succeeds. An failed assertion results a nil*.fixedinterface value. An failed assertion with the second optional result missing will panic.
- A type assertion on a
For this reason, the xyz ...interface{} parameter declarations of all the print functions
in the fmt standard package should be changed to xyz ...interface{}.fixed instead.
- Send
- We can send any values to a
*.mutablechannel. - We can send any values, which are convertible to
*.fixedvalues, to a*.fixedchannel.
- We can send any values to a
- Receive
- Receiving from a
*.mutablechannel results a*.mutablevalue. (It is not important whether or not the result itself can be modified.) - Receiving from a
*.fixedchannel results a*.fixedvalue. (It is not important whether or not the result itself can be modified.)
- Receiving from a
Function parameters and results can be declared with property {ref_modifiable: false}.
In the following function proptotype, parameter x and result w are viewed as being declared with var.fixed.
func fa(x Tx.fixed, y Ty) (z Tz, w Tw.fixed) {...}A func()(T) value is assignable to a func()(T.fixed) value (execpt T is an interace type), not vice versa.
A func(T.fixed) value is assignable to a func(T) value (execpt T is an interace type), not vice versa.
Yes, here the excetpion that the T can't be an interface type makes the rules some imperfect.
Many function and method implementations in the refect package should be modified accordingly.
The refect.Value type shoud have an fixed property,
and the result of an Elem method call should inherit the fixed property
from the receiver argument. More about reflection.
For all details on reflection, please read the following reflection section.
var x = []int{1, 2, 3}
var y [][]int.fixed
y = [][]int{x, x} // ok
x[1] = 123 // ok
y[0][1] = 123 // error, for y is a var.fixed value.
var z = y[0] // ok, z is also a var.fixed value.
z[0] = 123 // error
p := &z[0] // ok. p is a var.fixed value.
*p = 123 // error
x[0] = *p // ok
p = new(int) // ok
var v interface{} = y // error
var v interface{}.fixed = y // ok
var w = v.([][]int) // ok, w is a var.fixed value
v = x // ok
var u = v.(fixed) // ok, assertion succeeds. u is a var.fixed value
// S is exported, but external packages have
// no ways to modify x and S (through S).
final S = x.(fixed) // ok.
S = x // error
t := S[:] // ok, t is a var.fixed value. S[:] is a final.fixed value.
_ = append(t, 4) // error
// The elements of R even can't be modified in current package!
final R = []int{7, 8, 9}.(fixed)
// Q can't be modified, but its elements can.
final Q = []int{7, 8, 9}Another one:
var s = "hello word"
var bytes = []byte.fixed(s) // a clever compiler will not allocate a
// deplicate underlying byte sequence here.
{
pw := &s[6] // pw is a `var.fixed` value of built-in type "byte".
}I'm not familiar with the compiler development things. It is just my feeling, by my experience, that the rules mentioned in this proposal can be enforced by compiler without big technology obstacles.
At compile phase, compiler should maintain two bits for each value. One bit means whether or not the value itself can be modified. The other bit means whether or not the values referenced by the value can be modified.
reflect.Value values can only representing var.* Go values.
A reflect.FixedValueOf function is needed to create reflect.Value values representing var.fixed Go values.
Its prototype is
func FixedValueOf(i interface{}.fixed) ValueIn implementaion, one bit should be borrowed from the 23+ bits method number to represent the fixed proeprty.
All parameters of type reflect.Value of the functions and methods in the reflect package,
including receiver parameters, should be declared as var.fixed values.
However, the reflect.Value return results should be declared as var.mutable values.
A reflect.Value.ToFixed method is needed to make a reflect.Value value represent a var.fixed Go value.
A reflect.Value.FixedInterface method is needed, it returns a var.fixed interface value.
The old Interface method panics on var.fixed values.
A method reflect.Type.Fixed is needed to get the immutable version of a mutable type.
A method reflect.Type.Mutable is needed to get the mutable version of an immutable type.
The method sets of mutable type T and immutable type T.fixed may be different if T is not an interface type.
Their respective other properties should be identical.
A method reflect.Type.Genre is needed, it may return Fixed or Mutable.
This proposal doesn't guarantee some values referenced by *.fixed values will never be modified.
(This is more a feature than a problem.)
This proposal will make bytes.TrimXXX (and some others) functions need some duplicate versions
for mutable and immutable arguments.
This problem should be solved by future possible generics feature.
The followings are the incompatible cases I'm aware of now.
finalandfixedmay be used as non-exported identifiers in old user code. It should be easy for thego fixcommand to modify these uses to others. (Usingconstto replacefinalandfixedcan avoid this incompatible case, but may cause some confusions.)- Another incompatible case is caused by the fact that
*.mutableinterface value can't be assigned to*.fixedinterface values. When the parameters of a function, such as thefmt.Printfunction, are changed to immutable types, then some old user code will fail to compile. But it should be easy for thego fixcommand to modify the corresponding arguments to immutability assertions.