Is it possible to alias nil in Golang - go

While organizing my projects, I observe nil comparisons in many places. I wish to replace nil with NULL or Null. I respect Golang specs, but I am curious if we can do this.
I already did it for interface{} , context.Context as follows.
type CON = context.Context
type Any = interface{}

You cannot. What you show there are type aliases. nil is not a type. It is a value of a wide range of different types. You may hope that you can make a constant with a value of nil, similar to how you can make a constant of value 0, but this is explicitly disallowed by the compiler:
const NULL = nil
Error: const initializer cannot be nil
According to the language specification:
There are boolean constants, rune constants, integer constants, floating-point constants, complex constants, and string constants.
None of these types can have a nil value, therefore a nil constant is not possible.
You might also try to make a variable which holds the value nil, but you'll find the problem that it doesn't work if you don't declare the type of the variable:
var NULL = nil
Error: use of untyped nil
You can make it legal by adding a nilable type to the variable, but then it will no longer be very useful as it will only be comparable to that specific type.

Related

What's happening behind the scenes of Go's `nil` type?

I recently read on a popular programming forum that Go supports a handful of "typelesss" values — notably, nil, Go's null value/bottom type. I have fairly limited experience with Go, and one of the statements made on this forum caught me off guard — namely, that it's illegal to write x := nil in Go.
Sure enough, a toy Go program with that line doesn't compile and the compiler error clearly points out that the comment from the forum checks out: declaration and assignment of a variable to nil is disallowed. This seems like a bit of an odd limitation, but things get stranger.
It's a common idiom in Go to return partial errors by returning a tuple from a function. Something like this:
func might_error() (int, error) {
return 1, nil
}
func main() int {
x, err := might_error()
if err != nil {
panic("err was not nil!")
}
return x
}
This has at least two inconsistencies as far as I can tell:
First, even though nil is typeless on paper, it takes on the error type (by way of it implementing the Error interface combined with Go's duck typing) for the purposes of conforming to might_error's return type signature.
Second, it seems like nil is being used for that previously-illegal declaraiton-and-assignment in main, and in a situation where (at least in comparable languages) might_error could be treated as a constexpr.
Weirder still, replacing x, err := might_error() with x, err := 1, nil still errors out with use of untyped nil!
My current line of thinking is that the Error interface is injected into a specific instance of nil in cases where a function's type signature requires it, meaning that it stops being an untyped nil and becomes a typed nil for the lifetime of that specific nil instance, but I'm not at all sure that this is correct since it seems like a strange design choice by nature of it not being clearly generalizeable.
What motivated these design choices? Why have nil be untyped instead of having it be a proper null type, except when it's convenient, at which point it becomes a typed value implementing an Error interface (to the best of my understanding)?
This is a complete non-problem in the sense that in practice there can be confusion if a nil value is stored in an interface variable making that variable non-nil. This is https://golang.org/doc/faq#nil_error and everyone is bitten a few times by this problem until you learn that interface values containing a nil variable are no longer nil themself. It's a bit like with var s = []*int{nil, nil, nil} where s contains only nils but is non-nil itself.
Technically (from a language design point) you could introduce several "nils", e.g. nil for pointers, null for interfaces, noop for functions and vac for channels. (Exaggerating a bit). With this you could have:
type T whatever
func (t *T) Error() string // make *T implement error
var err error // interface variable of type error
print(err == null) // true, null is Zero-Value of interface types
print(err == nil) // COMPILER ERROR, cannot compare interface to nil (which is for pointers only
var t *T = nil // a nil pointer to T
err = t // store nil *T in err
print(err == null) // false err is no longer null, contains t
You could even remove the compiler error:
err = t // store nil *T in err
print(err == null) // false, err contains t
print(err == nil) // "deep" comparison yielding true
err = &T{} // store non-nil *T in err
print(err == null) // still false
print(err == nil) // false, value inside err is no longer nil
You also could define a default type for nil, e.g. *[]chan*func()string so that you can write x := nil like you can do with f := 3.141 where the default type of the untyped constant 3.141 is float64. But such a default type for nil would be arbitrary and not helpful at all (as my example shows; *[]chan*func()string is uncommon).
If I remember correctly there was a longer discussion about this topic on the golang-nuts mailng list where the rationals about this design was discussed. It boiled down to something like: "The actual real-life problems with having nil multiple meanings and not being a constant are tiny (basically just variants of an error type containing nil not being nil). The 'solution' to this one tiny problem would complicate the language (e.g. by introducing a null literal for the zero value of interface types) considerably. It probably is simpler to teach people that interface values containing a nil are no longer nil themself than introducing typed nils or nulls for interface types."
In more than 10 years of programming in Go I literally never had to think about a nil literal being typed or untyped or constant or whatever. The article you probably are referring to is constructing pure academic, but actually non-problem in practice issue out of a tiny design decision about having just one nil literal for all zero values for pointer, slice, map, channel and function types.
Addendum
First, even though nil is typeless on paper, it takes on the error type (by way of it implementing the Error interface combined with Go's duck typing) for the purposes of conforming to might_error's return type signature.
This is a completely wrong description of what happens.
If you write
func f() (r int) { return 7 }
then 7 is assigned to r of type int and f returns. This works because 7 can be assigned to int.
In
func might_error() (int, error) { return 1, nil }
the same happens, the second return variable of type error (an interface type) is set to nil because nil can be assigned to any interface type like you can assign nil to any pointer type or any function type.
This has nothing to do with "implementing the Error interface combined with Go's duck typing". Absolutely not. nil doesn't implement the error interface at all. Any interface value can be nil like can be any function value or slice value or channel value. Setting a chan to nil basically "clears" the channel variable, it doesn't mean that nil somehow "implements the channel interface". You seem to conflate the zero value of several types and how to set them by assigning nil with implementing interfaces. All this has basically nothing to do with nil being typed or not. The nil literal in source code os overloaded and often can be thought of as just representing the zero value of a type.
Your example doesn't compile (because main needs to return nothing), and when you go into picky and nitty areas of a language, all your examples should actually work. 😀
My current line of thinking is that the Error interface is injected into a specific instance of nil in cases where a function's type signature requires it ...
That's not quite right. The Go specification tells us how this works:
There are three ways to return values from a function with a result type:
The return value or values may be explicitly listed in the "return" statement. Each expression must be single-valued and assignable to the corresponding element of the function's result type. [example snipped]
This is the method you use in your example program (which I cleaned up a bit to compile and run in the Go Playground). The expression:
return 1, nil
means:
unnamed_return_variable_1 = 1
unnamed_return_variable_2 = nil
where the two unnamed return variables don't actually have these names (they're un-named, after all) but do have types, courtesy of the declaration of the function. So
return 1, nil
is not at all like:
x, err := 1, nil
but rather more like:
var x int; var err error; x, err = 1, nil
which, as you can see, is also quite valid.
What motivated these design choices?
For that, you'd have to ask the designers.
ThinkGoodly mentions in a comment that:
The untyped 0 does not cause this angst.
The untyped constant 0 has a default type, though:
An untyped constant has a default type which is the type to which the constant is implicitly converted in contexts where a typed value is required, for instance, in a short variable declaration such as i := 0 where there is no explicit type. The default type of an untyped constant is bool, rune, int, float64, complex128 or string respectively, depending on whether it is a boolean, rune, integer, floating-point, complex, or string constant.
The predeclared identifier nil has no type, not even a default type. It just has a series of special rules that allow it to be used in various places.
The non-language-canon way to think about this
While this isn't how it's defined (it's defined by the Go specification), this is how I recommend thinking about the issue:
The predeclared identifier1 nil is untyped.
There are typed nil values. Anything sufficiently pointer-like can be nil, or can be non-nil. For instance, any *int variable can be pointer-to-int-nil, as an uninitialized one is. This nil is not the nil "keyword" (predeclared identifier, see footnote); it's a completely different "nil", just like Bruce (Banner) is a completely different person from Bruce (Wayne).
Interface values are two part: a type, and a value of that type. The type can be nil! This is yet another kind of nil, different from nil (the untyped "keyword") and nil (some particular nil of some particular pointer-y type). If the type is nil, the value can also be nil. If both are nil, the interface value compares equal to the <nil,nil> pair that the untyped "keyword" nil turns into when needed for comparison against an interface value. If either one is non-nil, the comparison says these are not equal. The hardware implementation is that the two parts of the interface are both zero: if either part is nonzero, the thing as a whole is nonzero, and hence "not nil".
The conversion from nil-the-"keyword" (predeclared identifier) to an appropriate hardware style zero value occurs when there's enough information supplied by context. Assignment to some variable—even an unnamed one—supplies context. Assignment to some positional argument supplies context. Trying to use a short declaration fails to supply context, hence the error.
1Go in general favors predeclared identifiers to keywords, so that false and true and so on are not actually keywords. This keeps nil out of the keyword set as well. Nonetheless, some things—such as the boolean constants false and true, or the magical behavior of the predeclared identifier iota, are only accessible by not covering up the predeclared identifier and then using it. That is, if you name something iota, you can't get the iota style functionality until that name goes out of scope. The way these things behave is often handled via keywords in other languages. If you tend to think in, say, C++ or Java, you might want to keep reminding yourself that these aren't keywords, even though they smell like them.

What is the type of nil?

I want to check for nil in a type switch. What type do I use for nil in a switch case?
switch v := v.(type) {
case int:
fmt.Println("it's an integer", v)
case ????: <--- what type do I put here?
fmt.Println("it's nil!")
}
I want to check for nil in a type switch. What is the type of nil?
There is not a single place in the Go Language Specification that specifies nil, but there are multiple places that make it clear that nil has no type [bold emphasis mine]:
In the section on Variables:
unless the value is the predeclared identifier nil, which has no type
In the sub-section on Expression switches:
The predeclared untyped value nil […]
Even if the Go Language Specification did not explicitly say that nil has no type, it should at least be obvious that nil cannot possibly have a single type. At least not a type that is expressible within Go.
nil is a valid value for
pointers,
functions,
interfaces,
slices,
channels, and
maps.
This means that the type of nil must be all of those types at the same time. There are a couple of ways in which this could be achieved: The type of nil could be
a union type of all of those types,
an intersection type of all of those types (but that wouldn't be type-safe), or
a subtype of all of those types, or
a polymorphic type.
However, Go doesn't have union types, it doesn't have intersection types, it doesn't have subtyping, and it doesn't have polymorphic types, so even if nil had a type, this type would not be expressible in Go.
Alternatively, the designers of Go could declare that nil doesn't have one type, it has multiple types, but that wouldn't help you either: you would have to have cases for all the possible types nil can have, but those cases wouldn't apply only to nil. You would need to have a case for pointers, but that case would apply to any pointer not just to a nil pointer, and so on.
It is possible to check for a nil interface type, however. See the sub-section on Type switches:
Instead of a type, a case may use the predeclared identifier nil; that case is selected when the expression in the TypeSwitchGuard is a nil interface value. There may be at most one nil case.
Use nil to check for a nil interface value in a type switch:
switch v := v.(type) {
case int:
fmt.Println("it's an integer", v)
case nil:
fmt.Println("it's nil!")
}
Run it on the playground.
A nil interface value does not have a dynamic type. In the context of a type switch case, nil represents "no dynamic type".
You should check whether v is nil outside of your type switch.
if v == nil {
// Do stuff probably return or error out?
}
switch v := v.(type) {
// ...
}

Golang not able to detect implicitly typed const when assigning to a variable

When I try to assign an implicitly typed constant to a variable, the assigned variable doesn't detect the custom type and instead gets assigned as the underlying primitive type. i.e;
For:
type Custom string
const (
First Custom = "10"
Second = "20"
)
If I have a function:
func SomeFunc( x Custom) {
fmt.Printf("Inside func %v %v", x, reflect.TypeOf(x))
}
Then when I:
out := Second
SomeFunc(out)
it errors with:
cannot use out (type string) as type Custom in argument to SomeFunc
However SomeFunc(Second) works fine.
Also
fmt.Printf("%v %v\n",reflect.TypeOf(second),reflect.TypeOf(out)) //prints string string
Here is the reproducer: https://play.golang.org/p/Iv-C1ee992
Can someone help me understand what is happening here?
Second is an untyped const and has this property (https://blog.golang.org/constants):
An untyped constant is just a value, one not yet given a defined type
that would force it to obey the strict rules that prevent combining
differently typed values.
...
Assigning them to a variable of any type compatible with strings works
without error.
On the contrary out is a variable of type string. Again from the blog post:
and by now you might be asking, "if the constant is untyped, how does
str get a type in this variable declaration?" The answer is that an
untyped constant has a default type, an implicit type that it
transfers to a value if a type is needed where none is provided. For
untyped string constants, that default type is obviously string

What does nil mean in golang?

There are many cases using nil in golang. For example:
func (u *URL) Parse(ref string) (*URL, error) {
refurl, err := Parse(ref)
if err != nil {
return nil, err
}
return u.ResolveReference(refurl), nil
}
but we can't use it like this:
var str string //or var str int
str = nil
the golang compiler will throw a can't use nil as type string in assignment error.
Looks like nil can only be used for a pointer of struct and interface. If that is the case, then what does it mean?
and when we use it to compare to the other object, how do they compare, in other words, how does golang determine one object is nil?
EDIT:For example, if an interface is nil, its type and value must be nil at the same time. How does golang do this?
In Go, nil is the zero value for pointers, interfaces, maps, slices, channels and function types, representing an uninitialized value.
nil doesn't mean some "undefined" state, it's a proper value in itself. An object in Go is nil simply if and only if it's value is nil, which it can only be if it's of one of the aforementioned types.
An error is an interface, so nil is a valid value for one, unlike for a string. For obvious reasons a nil error represents no error.
nil in Go is simply the NULL pointer value of other languages.
You can effectively use it in place of any pointer or interface (interfaces are somewhat pointers).
You can use it as an error, because the error type is an interface.
You can't use it as a string because in Go, a string is a value.
nil is untyped in Go, meaning you can't do that:
var n = nil
Because here you lack a type for n. However, you can do
var n *Foo = nil
Note that nil being the zero value of pointers and interfaces, uninitialized pointers and interfaces will be nil.
nil is also a value but only difference is- it is empty.
In Javascript for the un-initialized variable will be undefined. In the same way Golang has nil as default value for all the un-initalized data types.
nil in Go means a zero value for pointers, interfaces, maps, slices, and channels.
It means the value is uninitialized
For the data types like slice and pointers which "refer" to other types, "nil" indicates that the variable does not refer to an instance (equivalent of null pointer in other languages)
It is thee zero value of the variables of type functions, maps, interfaces, channels and structures
nil is a predeclared identifier in Go that represents zero values for pointers, interfaces, channels, maps, slices and function types.
nil being the zero value of pointers and interfaces, uninitialized pointers and interfaces will be nil.
In Go, string can’t be nil. A string doesn’t qualify to be assigned nil.
a := nil // It will throw an error 'use of untyped nil'
var a *int = nil //It will work
In Go nil is predeclared identifier that represents nothing for pointers, interfaces, channels, maps, slices and function types. Uninitialized pointers and interfaces will be nil. A string doesn’t qualify to be either of the above-mentioned types and hence can’t be assigned nil.
If you declare a variable by shorthand decleration like this: variable:=nil this will give compile time error because compiler has no idea which type it has to assign to variable .
In Go map, slice and function doesn't support comparison but can be compared with nil.
If any two map, slice or function are assigned to nil and compared with each other then it will raise runtime error.
But if we assign nil to any two pointer variables and compare them with each other then no error will occur.
package main
import (
"fmt"
)
func main() {
var i *int =nil
var j *int =nil
if i==j {
fmt.Println("equal")
} else {
fmt.Println("no")
}
}
This code works fine and prints output as equal.
In Golang, datatype can use nil is pointers, interfaces, maps, slices, channels, function types and errors
in String value is ""
in INT value is ZERO
In Golang, zero value is the default value for defined variables. The default value of different datatypes are:
0 for integer types
0.0 for floating types
false for boolean types
"" for string types
nil for interfaces, slices, channels, maps, pointers and functions
So, nil is the zero value or default value for interfaces, slices, channels, maps, pointers and functions.
For example,
var a int
var s []int
var s1 []int = []int{1, 2}
fmt.Println(a == nil) // throw error
// invalid operation: a == nil (mismatched types int and untyped nil)
fmt.Println(s == nil) // true
fmt.Println(s1 == nil) // false
Since error is an interface, we can write
_, err := Parse("")
if err != nil { // if there is an error
// handle error
}

Nil slice vs empty slice and nil value comparison

I have read some text regarding nil slice vs empty slice. I believe I have some basic understanding of the differences between them.
Summary of my understanding: var instance []Type is nil slice and instance == nil returns true; while instance:=[]Type{} is empty slice and instance != nil
However this particular instance still puzzles me.
Please look at the link below for code. My question is the last 2 cases.
https://play.golang.org/p/udyHoOlSeP
Suppose I want to compare two slices, renamed type and interface matching and all. The instance where a receiver can be nil, even though it's not defined as copy by value; while the argument is copied by value, seems to
be non-nil as long as the argument is not untyped.
In the last 2 cases, receiver has been identified as nil while argument is being processed by := so it becomes an empty slice. (But the other == nil also reports false...) How can I fix this to satisfy the following requirement?
nilslice.Equals(nilslice) // -> true
Further, I tried to define another interface comparing to pointers of interface but failed. Compiler complains that
cannot use p (type *AnotherNullable) as type *PointerComparable in argument to AnotherNullable(nil).Equals:
*PointerComparable is pointer to interface, not interface
https://play.golang.org/p/wYO1GKcBds
How can I fix that?
EDIT: Thanks to #zippoxer for all the insights. I learned a lot. I hope new readers too, please don't forget to check out #zippoxer's comment in the answer too!
First, you don't need a pointer to an interface. An interface is already a pointer.
See Go: What's the meaning of interface{}?
Just change the Equals method to accept a PointerComparable instead of a *PointerComparable. Equals will accept an interface instead of a pointer to an interface, but you can still pass a pointer to a slice/whatever to it.
See https://play.golang.org/p/e_Gtq2oAFA
Second, the receiver Nullable isn't an interface, while the argument you pass to Equals is an interface. That would explain why the Nullable receiver stays nil and the Comparable argument isn't nil although it's underlying slice is. The thing is, the Comparable argument is an interface that points to something, so whatever it points to, it won't be nil.
This code explains the problem:
var a interface{}
fmt.Println(a == nil) // true, the interface doesn't point to anything
var someNilSlice []int
fmt.Println(someNilSlice == nil) // true, just some nil slice
a = someNilSlice
fmt.Println(a == nil) // false, now the interface does point to something

Resources