2

Let's say we have the following:

type I interface {
    M()
}

func main() {
    var i I
    i.M()
}

Why does Go not do some static analysis at compile type to check that the underlying value of the interface is nil and throw a compile time error? Instead we end up throwing a runtime exception at i.M()

==================================================================

Output:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0xffffffff addr=0x0 pc=0x2004d]

goroutine 1 [running]:
panic(0x13f480, 0x1040a038)
    /usr/local/go/src/runtime/panic.go:481 +0x700
main.main()
    /tmp/sandbox469152264/main.go:12 +0x4d
3
  • 2
    In this specific case it could be detected, but in general it can't be. A value of I is a non-constant value and thus can only be evaluated at runtime (not compile time).
    – icza
    Jun 5, 2016 at 13:39
  • If a language is a hammer, then it's the programmer's job to not hit his fingers with it. It's nonsense to expect the hammer to give you a warning, like "hey, you're running into trouble". There may be some tools to give you this warning in specific cases, but the compiler expects you to know what you're doing Jun 5, 2016 at 13:52
  • 1
    As @icza mentioned, it can't be detected in the general case. To add to that, if the compiler were to issue an error in this case, then it would be confusing to the programmer when they could sometimes rely on the compiler to catch this and other times not. Better to just always leave it for runtime so that the language's behavior is consistent and predictable.
    – joshlf
    Jun 5, 2016 at 14:39

1 Answer 1

1

Why does Go not do some static analysis at compile type to check that the underlying value of the interface is nil and throw a compile time error?

The zero value for interface is nil, and it is no more detected at compile time than any other nil value.
From the FAQ:

Under the covers, interfaces are implemented as two elements, a type and a value.

  • The value, called the interface's dynamic value, is an arbitrary concrete value and
  • the type is that of the value.

An interface value is nil only if the inner value and type are both unset, (nil, nil).
In particular, a nil interface will always hold a nil type.


This thread adds, regarding the nil value (runtime):

The basic answer is to never store something in an interface if you don't expect the methods to be called on it.
The language may allow it, but that violates the semantics of the interface.

To expound, a nil value should usually not be stored in an interface unless it is of a type that has explicitly handled that case in its pointer-valued methods and has no value-receiver methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.