zero value for generic types?

960 views
Skip to first unread message

Arthur Comte

unread,
Apr 20, 2022, 6:14:51 PM4/20/22
to golang-nuts
Hi! I was trying the following code, but it does not compile. How can I check if a generic value is its zero value. More broadly, can you explain where this issue comes from?
```go
type MemoryRepository[E identifiable.Identifiable] struct {
  elements []E
}

func (repo MemoryRepository[E]) Find(ID string) (E, error) {
  elem, _ := repo.findWithIndex(ID)
    if elem == nil {
      return elem, fmt.Errorf("element with id '%v' does not exist", ID)
    }
  return elem, nil
}
```
I should (and will) just use an error to check for proper execution, that code was written pretty late in the night!
But now I'm curious about the semantics this code butts against

Arthur Comte

unread,
Apr 20, 2022, 6:30:07 PM4/20/22
to golang-nuts
Actually, even with proper error handling, I still need to return a value. In some functions I can just return a variable that was defined in the function, but that is not always available. In those cases, the only solution I've found is to use `*new(E)`, which seems plain terrible. Is there an alternative? I'm guessing something is wrong with my angle

Ian Lance Taylor

unread,
Apr 20, 2022, 6:46:17 PM4/20/22
to Arthur Comte, golang-nuts
On Wed, Apr 20, 2022 at 3:30 PM Arthur Comte <ad...@arthurcomte.com> wrote:
>
> Actually, even with proper error handling, I still need to return a value. In some functions I can just return a variable that was defined in the function, but that is not always available. In those cases, the only solution I've found is to use `*new(E)`, which seems plain terrible. Is there an alternative? I'm guessing something is wrong with my angle

See https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#the-zero-value

The easy is verbose way is

var zero E

Ian

roger peppe

unread,
Apr 21, 2022, 10:16:49 AM4/21/22
to Arthur Comte, golang-nuts
On Wed, 20 Apr 2022 at 23:29, Arthur Comte <ad...@arthurcomte.com> wrote:
Actually, even with proper error handling, I still need to return a value. In some functions I can just return a variable that was defined in the function, but that is not always available. In those cases, the only solution I've found is to use `*new(E)`, which seems plain terrible. Is there an alternative? I'm guessing something is wrong with my angle

FWIW I've been using `*new(E)` throughout, on the understanding that sooner or later there will be a proposal implemented that makes that simpler, and when that does happen, it'll be trivial to do  `gofmt -w -r '*new(x) -> zero` (for whatever spelling of "zero" is chosen), leaving the code exactly as you'd have wanted to have written it in the first place with no need for manual tidy-ups. Yes, it's crude for the time being, but at least it's unambiguous.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAP1AJ8fqT0yxd2g8gctPA%2B6Ei-fKfFQCdDM4iBMtfX%2BS-r9qgg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages