Skip to content

proposal: Go 2: Add typednil keyword for checking whether an interface value is a typed nil. #24635

Closed
@jaekwon

Description

@jaekwon

Following from this proposal: #21538, I think an alternative proposal would be to "simply" introduce a new keyword called typednil. It can only used for comparison, and it would have behavior identical to the following code:

func IsTypedNil(o interface{}) bool {
	rv := reflect.ValueOf(o)
	switch rv.Kind() {
	case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice:
		return rv.IsNil()
	default:
		return false
	}
}

This way, the current nil keyword behavior is preserved -- a zero value for interfaces as well as channels, funcs, maps, pointers, and slices, but we could now do the following:

var a interface{} = (*foo)(nil)
a == nil // false
a == typednil // true
var b *foo = nil
var b *foo = typednil // compile time error
b == typednil // compile time error

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions