Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yeah but that's because the zero value for slices is an empty slice, not a nil slice. This is what we should have more of.

If in the proposal (x==nil) == false then we get the ridiculous position where:

    var x int
    x=nil
    if x==nil {
        //this never happens despite having just set x=nil
    }
which seems ridiculous.

Though the other case, of returning true, also seems ridiculous, because nil and 0 are not the same thing at all.



The difference between an empty slice and a nil slice is one of the most inexplicable (to me) warts of the language, especially since it doesn't apply to anything else (maps can't be nil, nil and empty interfaces actually have different properties).

Anyway, I do agree that using `nil` as a base case for all types would introduce more confusion. What I think could be useful would be a `default` keyword for this:

  var x int
  x = default
  if x == default {
    this is true
  }
This would obey rules similar to `iota` in terms of its integration into the language.


Yeah that would work better than using nil. And it would make it clear that it was being initialised to whatever the default value for that type is.

Though, of course, most coders would leave out the second step here since the compiler initialises all variables with their type's default value ;)


Sure :), the really nice example would be

  return default, default, default, err


This simply would not compile because of "(mismatched types int and nil)". Solved already.

That's why I actually would prefer the "_" syntax:

    var x int
    x = _
    _ = 42

    if x == _ {
        // this happens even though you've assigned 42 to _, yes
    }




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: