Hi Chad,

On Sat, Jul 2, 2016 at 10:43 AM, Chad <send2b...@gmail.com> wrote:
>
> On Saturday, July 2, 2016 at 10:23:04 AM UTC+2, Martin Geisler wrote:
>>
>> On Fri, Jul 1, 2016 at 4:01 PM, Chad <send...@gmail.com> wrote:
>>>
>>> On Friday, July 1, 2016 at 3:44:10 PM UTC+2, Martin Geisler wrote:
>>>> I keep seeing references (hah!) to this concept of a "reference type"
>>>> :-) However, I just tried searching the language spec and Effective Go
>>>> and there doesn't seem to be such a concept defined in those
>>>> documents.
>>>
>>> I think it should. It is mentioned here however
>>> https://blog.golang.org/go-maps-in-action
>>
>> You're right, that article calls maps, slices and pointers "reference
>> types".
>>
>> I feel that is a little unfortunate since it muddles the picture and
>> makes the implementation more obscure. I would have been happy to have
>> been told right from the start that a slice is a small struct, small
>> enough that you can pass it by value instead of with a pointer. That
>> allows me to build a mental model in terms of other Go constructs.
>
>
> A struct is considered a reference type if at least one of the field
> *points* to another object. (i.e. holds a reference to another object).
> https://en.wikipedia.org/wiki/Reference_type

That is not how I've seen the word "reference type" used and I don't
think that's what the Wikipedia article tries to say. As I read it, it
says that a language like C++ has some types that are value types
(int, bool, structs, classes) and some types that are reference types
(&int, &bool).

As far as I know, reference types only show up in function and method
signatures in C++. It specifies that the argument should be passed by
reference instead of being copied like normal -- the compiler will
typically handle this by changing the &int parameter to a *int (int
pointer) parameter, insert & at the call site and * inside the
function. So the reference type gives you almost the same as a
pointer, but without the explicit dereferencing.

Other languages like Java and Python have reference types too: in
Java, all object instances are reference types. So when you pass an
object to a method, you pass a reference and modifications done in the
method are visible after the call.

I believe C# allows you to specify if you want a class to be a
reference type or a value type. The Wikipedia article says you use
"struct" for value types and "class" for reference types. This matches
how "struct" gives you a value type in Go.

The mail from as.utf8 points to this discussion (thanks!):

  https://groups.google.com/forum/m/#!topic/golang-dev/926npffb6lA

which points to this issue:

  https://github.com/golang/go/issues/5083

They make it pretty clear that people have been trying to remove the
use of "reference type".

> It's clearer. "small struct" would not be explicit enough nor true.
> I think that slices require typically more documentation effort to clarify
> what they are. Then, the issue of comparability will be obvious.
>
> There are user-defined reference types too.
>
> type Foo struct{
>    name string
>    data *[192]byte
> }
>
> That would be a reference type. This one is comparable.

I don't think that's a reference type in the usual sense of the word.
The basic test for me is what happens when you pass a Foo to a
function:

  func ChangeTheFoo(f Foo) {
      f.name = "changed"
  }

if the name field is changed after the call, then Foo is indeed a
reference type. However, it won't be changed in Go since the Foo
struct is copied when the function is called and this is why I say
that Foo is not a reference type. That is true of all structs in Go.

-- 
Martin Geisler

-- 
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