2 ms·
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 anythi
by simiones 5y ago
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.
- marcus_holmes 5y agoYeah 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 ;)
- simiones 5y agoSure :), the really nice example would be return default, default, default, err