News for hackers. Stuff that matters.
Re:Having fun with Go's nil, interfaces and errors by Vendan on Saturday September 17, 2016 @02:07PM (#12521618)
Except it's not a language implementation detail, it's part of what the language is. You might as well ask why a user of another language needs to know what a class is, or how inheritance works. Too many people seem to be assuming "Oh, it's like void*, so it must act like that always!", but it's not.
Re:Having fun with Go's nil, interfaces and errors by sdegutis on Saturday September 17, 2016 @03:33PM (#12522034)
After using Go for a few years, I've learned (the hard way) that Go does not separate the concepts of implementation details and the official language specification. The official implementation is the language, and they're inseparable. This is why a lot of design decisions about the language make you say "wait, what? why does it need to be that way?" and the community's official answer is "because that's how it works best with how it's implemented".
Re:Having fun with Go's nil, interfaces and errors by woah on Saturday September 17, 2016 @03:50PM (#12522115)
In the end, you are programming a computer.
Re:Having fun with Go's nil, interfaces and errors by catnaroek on Saturday September 17, 2016 @04:03PM (#12522189)
Yes, but the whole point to using abstractions (such as high-level languages) is to separate the context where implementation details are relevant (e.g., when writing a compiler), from the rest of the system, where implementation details don't matter (e.g., when using a compiler).
Re:Having fun with Go's nil, interfaces and errors by brandonbloom on Saturday September 17, 2016 @04:50PM (#12522389)
It's a stunningly difficult and surprisingly low-value proposition to create abstractions that don't leak. That's not the goal anyway: The goal is to leverage abstractions to build things. The implementation details frequently do matter. Even if your abstraction is airtight, it has to run on a real machine. When possible, it's preferable to design abstractions that have a clear implementation in terms of the lower level's abstractions. This way, when something goes wrong - and it will - you can understand and fix it.
Re:Having fun with Go's nil, interfaces and errors by pcwalton on Saturday September 17, 2016 @05:26PM (#12522547)
The behavior of Go's nil does not make the language any simpler to implement in any meaningful sense. The only thing it affects at the machine level is which cmp instructions are generated.
Re:Having fun with Go's nil, interfaces and errors by brandonbloom on Saturday September 17, 2016 @07:54PM (#12523169)
I won't defend the weird behavior of equality comparisons to nil, that definitely seems wrong. I was mostly just addressing the notion that a language needs a formal specification and must fully abstract the machine to be useful or at least not worthy of ridicule.
Re:Having fun with Go's nil, interfaces and errors by catnaroek on Saturday September 17, 2016 @05:34PM (#12522583)
> It's a stunningly difficult and surprisingly low-value proposition to create abstractions that don't leak.

Maybe for you. For me, non-leaky abstractions have enormous value. And, while designing non-leaky abstractions requires more effort upfront, in the long run, it requires less effort than plugging leaks in carelessly designed (non)abstractions.

> That's not the goal anyway: The goal is to leverage abstractions to build things.

The goal is to do it efficiently, and creating as few problems as possible for the future.

> The implementation details frequently do matter.

Of course they do matter - for the implementor.

> When possible, it's preferable to design abstractions that have a clear implementation in terms of the lower level's abstractions.

No disagreement here.

> This way, when something goes wrong - and it will - you can understand and fix it.

That's the job of the abstraction's implementor. If that happens to be me, I will fix it. Otherwise I'll just submit a bug report. If that doesn't work, then I'll reimplement the abstraction myself, but by no means will I work around an existing broken implementation.

Re:Having fun with Go's nil, interfaces and errors by mappu on Saturday September 17, 2016 @07:25PM (#12523068)
Do you have any examples of this?
Re:Having fun with Go's nil, interfaces and errors by catnaroek on Saturday September 17, 2016 @02:39PM (#12521768)
> Except it's not a language implementation detail, it's part of what the language is.

Okay, but what was the rationale for designing the language like that?

Re:Having fun with Go's nil, interfaces and errors by jerf on Saturday September 17, 2016 @03:36PM (#12522048)
The last bit of information you need to understand that is that unlike some other languages you may be used to, "nil" is a perfectly valid value for a pointer to have. You can write methods like this:

    func (o *Object) ReturnSomething() int {
        if o == nil {
            return 0
        }
        return o.SomeOtherValue
    }
It is also therefore legal to return in an interface value a nil pointer to a struct of a particular kind, and therefore "an interface containing a nil pointer of a particular type" and "an interface containing nothing at all" are fundamentally different things that can not be collapsed together.

So as Vendan says, it is just part of the language. All languages have this sort of wart in them, where two or more perfectly sensible decisions interact to create something that isn't sensible at first glance.

(I'm still in favor of going back in time and changing Go to have non-nillable values, but that ship has certainly sailed.)

Re:Having fun with Go's nil, interfaces and errors by pcwalton on Saturday September 17, 2016 @05:34PM (#12522578)
Java and C# also have interfaces and they don't have this strange behavior around null. I don't see how it's any sort of natural consequence of having null plus interfaces. Rather I suspect the reason is that the type equality operator was implemented to check one word and not the other when comparing two-word interface types, and the behavior that resulted was eventually specified instead of changing the implementation to compare both words.
Re:Having fun with Go's nil, interfaces and errors by catnaroek on Saturday September 17, 2016 @04:17PM (#12522241)
> unlike some other languages you may be used to,

The languages I'm used to don't even have `nil`.

> "nil" is a perfectly valid value for a pointer to have.

So, just like C and Java. I don't see the difference.

> You can write methods like this: (snippet)

Oh, now I get it: `nil` itself isn't a value. `nil` is syntactic sugar that expands into a nil value. (In other words, confusingly enough, `nil` and “nil value” are different things!) This is unlike Java, where `null` itself is a value.

It would be very helpful if people described things precisely.

> All languages have this sort of wart in them, where two or more perfectly sensible decisions interact to create something that isn't sensible at first glance.

This means that at least one of the two (or more) decisions was less sensible than it originally seemed. Good design decisions don't introduce warts into a system.

Re:Having fun with Go's nil, interfaces and errors by Vendan on Saturday September 17, 2016 @04:46PM (#12522366)
nil doesn't "expand", it's just what it is. You have to bear in mind that methods are roughly syntactic sugar from

  func (o *Object) ReturnSomething() int
to

  func  ReturnSomething(o *Object) int
In the first case, it may "seem" that a nil Object would be invalid, but in the second case it's obviously valid, and makes complete sense. It may be described as a "wart", but it makes complete sense to me. The issue is, in my opinion, that people drag their own expectations into programming languages as they learn them.
Re:Having fun with Go's nil, interfaces and errors by pcwalton on Saturday September 17, 2016 @05:35PM (#12522588)
In this case, Java and C# have the same feature and don't have this behavior around null. I think it's reasonable for people who have seen identical features in other languages to expect similar behavior when learning new ones.
Re:Having fun with Go's nil, interfaces and errors by Vendan on Saturday September 17, 2016 @05:50PM (#12522670)
They don't have the same feature of "I can call a method on a null value", at least, not that I've seen... You are calling them identical features when the fact is that they aren't. C# and Java both have generics, do they perform identical? Haskell has generics too, but I bet they'd throw a shit-fit if you tried to say "Oh, C# has generics too, so why would I bother with Haskell?"
Re:Having fun with Go's nil, interfaces and errors by pcwalton on Saturday September 17, 2016 @06:51PM (#12522941)
The ability to call a method on a nil receiver doesn't have anything to do with what comparisons should do. Semantically, the question is whether == should compare the values or the vtables. In no other language I know of does it compare vtables.
Re:Having fun with Go's nil, interfaces and errors by prodigal_erik on Saturday September 17, 2016 @07:03PM (#12522992)
If x.Foo() and y.Foo() do different things and return different values, clearly x != y, and golang needs some kind of SQL-style is-null check rather than using equality to check for null.

But dynamic dispatch on "what type of object do I not have?" is still a damn weird thing to want.

Re:Having fun with Go's nil, interfaces and errors by slrz on Sunday September 18, 2016 @08:28AM (#12525014)
Interface comparisons check both, the value and the dynamic type. Describing that as "comparing vtables" is misleading.

It the types don't match, the operands are not considered equal. Type and value have to match for two interface values to be equal. How does that not make sense?

For mismatched dynamic types you can't meaningfully compare the values in the general case, so of course you need to consider types.

Re:Having fun with Go's nil, interfaces and errors by pcwalton on Sunday September 18, 2016 @12:39PM (#12525926)
> Type and value have to match for two interface values to be equal. How does that not make sense?

Because people expect == to compare values, not types. Nobody has ever questioned this behavior in Java and C#.

Re:Having fun with Go's nil, interfaces and errors by Vendan on Saturday September 17, 2016 @07:47PM (#12523143)
Having dealt with languages that don't, I much prefer ones that do. The thing is, most languages DO basically check vtables, in that 2 values with different types will never be equal (unless there is implicit casting or some kind of equality override). For instance, 1 == "1" is valid, and true, in PHP, even though that is less then ideal. The difference, of course, is that most of those checks are done at compile time in many languages. Go will do some checks at compile time, but it's specifically unable to do some of them due to the "dynamics" of how interface variables work.
Re:Having fun with Go's nil, interfaces and errors by catnaroek on Saturday September 17, 2016 @06:19PM (#12522813)
You can call a method on a null value in Java and C#. It's perfectly defined behavior: throwing a NullPointerException.
Re:Having fun with Go's nil, interfaces and errors by Vendan on Saturday September 17, 2016 @06:26PM (#12522845)
yes, and it's a completely different behavior then Go, where it's perfectly defined as well: you call that method with a nil pointer as the receiver. Please don't act like those 2 behaviors are anywhere near similar.
Re:Having fun with Go's nil, interfaces and errors by zeeboo on Saturday September 17, 2016 @03:34PM (#12522040)
An interface being equal to nil just because the value it contains is nil would be even more confusing. For example, imagine if you had two interface values, one holding a nil * T and one holding a nil * S. They would both be equal to nil, but not equal to each other, losing transitivity of equality.

Also, sometimes nil is a perfectly valid value of something and can implement interfaces. You can call methods on nil receivers because method calls are just curried function applications over the value. It would be odd if the interface compared equal to nil just because I happened to implement it with a value that's valid with nil.

Re:Having fun with Go's nil, interfaces and errors by katcipis on Saturday September 17, 2016 @06:10PM (#12522766)
I had not thought about transitivity on equality, now it makes even more sense to behave like this. Thanks.
Re:Having fun with Go's nil, interfaces and errors by twic on Saturday September 17, 2016 @08:26PM (#12523262)
Couldn't they also be equal to each other? If equality was defined only in terms of the value and not the vtable (to borrow pcwalton's terms), they would be equal. Are there any problems which would arise from this?
Re:Having fun with Go's nil, interfaces and errors by slrz on Sunday September 18, 2016 @08:46AM (#12525072)
Equality is not necessarily defined for values of different types ("with different vtables", if you will). You can't just compare values of different type and expect a meaningful result.
Re:Having fun with Go's nil, interfaces and errors by twic on Sunday September 18, 2016 @03:18PM (#12526708)
In this case, though, we're talking about interfaces; an interface is a pointer plus this bit we're calling a vtable. Pointers, even to different types, absolutely can be compared for equality, can't they? More specifically, i am pretty sure pointers can be compared to see if they are both nil, which is all that would be needed here.
Re:Having fun with Go's nil, interfaces and errors by cakoose on Saturday September 17, 2016 @03:31PM (#12522019)
Sometimes you try and make one feature work a certain way and that decision has unintended consequences. Most languages have surprises like this.

In this case, it's an implicit conversion from "nil" to the "nil value" of a given type. This is convenient in many cases, but causes confusion in other cases.

Re:Having fun with Go's nil, interfaces and errors by kjksf on Saturday September 17, 2016 @04:58PM (#12522424)
In Go, it's called "zero value", not "nil value".

It's also not an implicit conversion from nil to something. Nil isn't a value the way 0 is a value of an integer or "foo" is a value of string.

Per Go spec (https://golang.org/ref/spec) nil is "predeclared identifier which has no type". That implies it doesn't have a value and therefore cannot be converted to a value.

Nil is zero value of pointer types (which in practice end up being a pointer whose value interpreted as integer is 0) and also a zero value of interface type (which is interface without a type and without a value) and a few other types.

Re:Having fun with Go's nil, interfaces and errors by catnaroek on Saturday September 17, 2016 @04:47PM (#12522370)
> In this case, it's an implicit conversion from "nil" to the "nil value" of a given type.

Upvoted because of this, because it clarified my earlier confusion. That being said, I don't really agree with the rest.