Skip to content

proposal: Go 2: introduce interface type to solve nil comparison dilemma #43269

Closed
@marcosrmendezthd

Description

@marcosrmendezthd

There's a lot of discussion regarding comparing interfaces to nil (https://golang.org/doc/faq#nil_error, #22729). I recently had to deal with this, and as a developer, I'm surprised we haven't had more issues since errors are interfaces. It is not a trivial issue to solve.

My humble suggestion is to introduce another type with new semantics.

We would migrate the error type so that err != nil or err == nil always works. The new type would be interoperable with a regular or typed interfaces. It should essentially work the same except for comparisons, where the new semantics could then be introduced.

type error new_type {
   Error() string
}

Developers could then start migrating their code to use this new type as needed, so that existing code doesn't have to be changed immediately and we don't introduce any breaking changes.

type new_typed_interface new_type {
  ...
}

y := getLegacyTypedInterface().(new_typed_interface)

if y != nil {
...
}

Thoughts? Comments?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions