-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: add package ptr provide pointer functions #61082
Comments
previously #38298 (comment) |
Most of these functions appear to be confusing pointer equality with equality of the things pointed to. |
This is pretty similar to https://github.com/carlmjohnson/pointer. Why not just use my third party package instead of having it in the standard library? |
I didn't know it existed until you posted it just now, nor would I have thought to go looking for a package like this. If I wanted a function like one of these, I'd just write it in whatever package I needed it in. I'd probably wind up writing it quite a few times across multiple projects. I think some of the function names and godoc comments as proposed are confusing and/or misleading, but I think that I generally agree with the idea of the proposal. A lot of these are potentially useful in a lot of contexts and seem as good a fit for the standard library to me as the new |
#45624 is a proposal to add this capability to the language directly. |
I didn't know about this library either until you pointed it out. Without it in std library, I prefer to implement it myself instead of searching on GitHub. It is commonly used but not complicated to implement. I also have an implementation at ptr, but how many people know about it until I bring it up? |
English is not my mother tongue, so there may be some expressions that are not clear in English. But these functional implementations are explicit. |
There is indeed some functional overlaps here, but cannot be completely replaced by #45624. #45624 need language change, but this one don't need. Although it has been proposed for two years, there is still no key progress This also provide other functions that #45624 not contains, such as handle default value, comparison and equality. In addition, default value handle, in some cases, can replace the ternary operator. Consider the following situation: func y(x *int) {
v := 100
if x != nil {
v = *x
}
}
// can simplify to
func y(x *int) {
v := ptr.ValueOr(x, 100)
}
|
In go, we can't, but I think this should be another issue to discuss type constraint. I had initiated discussion at golang-nuts, but not many people have paid attention to this question, and did not get the reply from the official team. https://groups.google.com/g/golang-nuts/c/uGU7sbKqVZ4/m/9qxpPN8jAQAJ |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
Pointer is widely used in go, pointer also provide optional semantic, and We often need to deal with the default value of the pointer and the operation of the underlying data, such as: comparing the underlying data, etc.
Since we have generics now, so provide a package to solve those problems are useful and convenient.
Some possible APIs are as follows:
The text was updated successfully, but these errors were encountered: