this post was submitted on 18 May 2024
357 points (97.6% liked)

Programmer Humor

19503 readers
1246 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 

Good price.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 5 months ago (1 children)

If nil ≠ nil, how do you compare a variable to the literal?

[–] [email protected] 1 points 5 months ago (1 children)

You'd first check for nil values, then compare like normal. Extra step, yes, but it keeps you from hitting NPEs through that route.

[–] [email protected] 1 points 5 months ago (1 children)

You'd first check for nil values

What does this mean, if not the same as

then compare like normal

?

[–] [email protected] 1 points 5 months ago (1 children)

IIRC, a nil value can be checked against a literal successfully but not against another nil value. Say you want to check for equality of two vars that could be nil. You just need an extra if statement to ensure that you are not trying to compare nil and nil or nil and a non-nil value (that'll give you a type error or NPE):

var a *string
var b *string

...
if a != nil && b != nil {
  if a == b {
    fmt.Println("Party!")
  } else {
    fmt.Println("Also Party!")
}
[–] [email protected] 1 points 5 months ago* (last edited 5 months ago) (1 children)

What I mean is that in JS you can't do NaN != NaN, not even variable != NaN. So you're not saying it's the same in Go, since you can do a != nil?

[–] [email protected] 1 points 5 months ago* (last edited 5 months ago) (1 children)

Kinda. nil is a weird value in Go, not quite the same as null or None in JS and Python, respectively. A nil value may or may not be typed and it may or may not be comparable to similar or different types. There is logical consistency to where these scenarios can be hit but it is pretty convoluted and much safer, with fewer footguns to check for nil values before comparison.

I'm other words, in Go (nil == nil) || (nil != nil), depending on the underlaying types. One can always check if a variable has a nil value but may not be able to compare variables if one or more have a nil value. Therefore, it is best to first check for nil values to protect against errors that failure to execute comparisons might cause (anything from incorrect outcome to panic).

ETA: Here's some examples

// this is always possible for a variable that may have a nil value. 
a != nil || a == nil

a = nil
b = nil
// This may or may not be valid, depending on the underlying types.
a != b || a == b

// Better practice for safety is to check for nil first
if a != nil && b != nil {
    if a == b {
        fmt.Println("equal")
    } else {
        fmt.Println("not equal")
    }
} else {
    fmt.Println("a and/or b is nil and may not be comparable")
}
[–] [email protected] 2 points 5 months ago (1 children)

Thoroughly confusing lol. I think I need to check the spec in order to grasp this. I feel like this has more to do with the typing system rather than nil itself, maybe. I'll see.

But yeah, this is nothing like null or undefined in JS, but more similar to NaN.

Thank you for trying to explain!

[–] [email protected] 2 points 5 months ago

Yeah... It's weird but I find it useful that it is, in a weird way. Treating it as an uncertainty means that one MUST explicitly check all pointers for nil as part of normal practice. This avoids NPEs.