← Back to context

Comment by thomashabets2

1 day ago

Author here. No, I didn't misunderstand it. Interface variables have two types of nil. Untyped, which does compare to nil, and typed, which does not.

What are you trying to clarify by printing the types? I know what the types are, and that's why I could provide the succinct weird example. I know what the result of the comparisons are, and why.

And the "why" is "because there are two types of nil, because it's a bad language choice".

I've seen this in real code. Someone compares a variable to nil, it's not, and then they call a method (receiver), and it crashes with nil dereference.

Edit, according to this comment this two-types-of-null bites other people in production: https://news.ycombinator.com/item?id=44983576

> Author here. No, I didn't misunderstand it. Interface variables have two types of nil. Untyped, which does compare to nil, and typed, which does not.

There aren't two types of nil. Would you call an empty bucket and an empty cup "two types of empty"?

There is one nil, which means different things in different contexts. You're muddying the waters and making something which is actually quite straightforward (an interface can contain other things, including things that are themselves empty) seem complicated.

> I've seen this in real code. Someone compares a variable to nil, it's not, and then they call a method (receiver), and it crashes with nil dereference.

Sure, I've seen pointer-to-pointer dereferences fail for the same reason in C. It's not particularly different.