Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: pointer: new package for pointer allocation and dereference #64749

Closed
matthinrichsen-wf opened this issue Dec 15, 2023 · 6 comments
Closed
Labels
Milestone

Comments

@matthinrichsen-wf
Copy link

matthinrichsen-wf commented Dec 15, 2023

Proposal Details

A project I'm working on currently makes heavy use of utilities that:

  1. Safely Dereference a pointer:

ex 1.

func ToValue[T any](p *T) (T, bool) {
   if p == nil {
       var t T
       return t, false
   }
   return *p, true
}

ex 2.

func ToValueWithNil[T any](p *T, valueIfNil T) T {
   if p == nil {
       return valueIfNil
   }
   return *p
}
  1. Generically Convert a Value to a Pointer:

ex.

func FromValue[T any](value T) *T {
 return &value
}

It would be nice to have utilities like these in the standard library as they seem generally useful.

  1. Compare Pointers and Their Values:

ex.

func ValuesEqual[T comparable](p1, p2 *T) bool {
  if p1 == nil || p2 == nil {
     return p1 == p2
  }
  return *p1 == *p2
}
@gopherbot gopherbot added this to the Proposal milestone Dec 15, 2023
@seankhliao
Copy link
Member

we just declined #61082

@matthinrichsen-wf
Copy link
Author

@seankhliao I see. I searched for prior art, but must not have looked at closed proposals.

@seankhliao seankhliao changed the title proposal: "pointer": Generic Pointer Utilities proposal: "pointer": pointer value wrapping / unwrapping Dec 17, 2023
@adonovan
Copy link
Member

These functions seem easy enough to define in the relatively few places where they are heavily needed.

Also, ToValueWithNil is strict in both arguments, whereas the more useful semantics (in my experience) are lazy: ptr ? *ptr : computeDefault() in C conditional notation.

@matthinrichsen-wf
Copy link
Author

matthinrichsen-wf commented Dec 20, 2023

@adonovan Fair points.

As far as ToValue* are concerned, I think they would be obviated by a safe-deference (like we have for safe map accesses):

value, ok := *somePointer
if !ok {
    value = someValueIfNil
}

(which I would also love to have instead tbh)

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Jan 2, 2024
@rsc
Copy link
Contributor

rsc commented Jan 10, 2024

@rsc rsc changed the title proposal: "pointer": pointer value wrapping / unwrapping proposal: pointer: new package for pointer allocation and dereference Jan 10, 2024
@rsc rsc moved this from Incoming to Declined in Proposals Jan 10, 2024
@rsc
Copy link
Contributor

rsc commented Jan 10, 2024

No change in consensus, so declined.
— rsc for the proposal review group

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Declined
Development

No branches or pull requests

5 participants