Skip to content

proposal: builtins: add zero #52307

@earthboundkid

Description

@earthboundkid

Every individual type can be compared to its zero value (func() == nil, for example), but there is no way to write this code today without using package reflect:

func IsZero[T any](t T) bool {
  return t == zero
}

It must instead be:

func IsZero[T any](t T) bool {
  return reflect.ValueOf(&t).Elem().IsZero()
}

I propose adding a built in constant zero that can be tested for equality with any type.

It could also be returned, as in

if err != nil {
  return zero, err
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions