I like Alan's "useful definition" of a semantically reference type, which 
involves shared mutable state.

About the string type, I also like the implementation detail that
  
var string t = s

is O(1) in memory and running time, i.e. it happens internally to "copy the 
reference, not the bytes" :  see https://play.golang.org/p/p-PD_yOnw6
It usually doesn't make much of a difference because most strings are 
short, and also they are not the best data structure to manipulate data 
(unlike []byte). But I take this implementation detail into account when I 
want to optimize code, especially to reduce the need for memory allocation.

On Wednesday, October 19, 2016 at 6:44:26 PM UTC+2, adon...@google.com 
wrote:
>
>
> Jan is write that the term does not appear in the spec, but I think it's 
> possible to come up with a useful definition of a reference type that 
> applies to all ALGOL-like languages: a reference type is one whose values 
> indirectly refer to mutable state.  So, pointers are obviously references, 
> as are slices, maps, and channels.  But a string is not a reference 
> because, although internally it contains a pointer, you cannot mutate the 
> array of bytes to which it refers.  Functions may be references, because a 
> closure may refer to lexically enclosing variables.  Structs and arrays are 
> references if their elements contain references.  An interface value is a 
> reference if its payload contains a references.
>
> The essence of a reference is that copying one creates a new alias for its 
> underlying state, and changes made via one alias are visible to all others. 
>  This definition is not absolute: a pointer to an immutable data structure, 
> for example, can be considered to have "value" (non-reference) semantics 
> since although it points to a variable, that variable can never be changed.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to