-
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: "?" as "!= nil" #32845
Comments
It's one of the better ideas I've seen here, and I don't hate it. I still think it adds a little obscurity where we otherwise had more explicit purpose. Explicit is better than implicit. |
I like the intent of this proposal but I'm not a fan of the package main
func main() {
var e error
var v *struct{ int }
if nil(e) {
// e is nil
}
if nil(v) {
// v is nil pointer
}
} The above checks ( This requires no new syntax, reads clearly (literally "if nil (expr)" or "if not nil (expr)" when paired with |
I think you overlooked #32611 |
could we extent this to just this?
v will return true if the value is non-zero. This will be compatible with the current use of |
@JAicewizard, we won't do It is a feature that the compiler complains when you forget to write half the comparison. |
@kybin, this is an interesting idea, thanks. Not sure about the readability of There are significant fixed costs to any language change: updating tools, teaching everyone the new thing, updating tools, deciding when it is appropriate, teaching everyone those guidelines, and so on. The change needs to bring benefits that pay for those costs. Some people are saying that try does not bring enough benefits to outweigh these costs, but |
err? {
return err
} '?' already have meaning of 'if' |
There are only a few printable ASCII characters that Go doesn't use today. To me it doesn't seem worth using one of them just to avoid writing |
@rsc and @ianlancetaylor What if this is not a "just" non-equal to I vaguely remember we have had our part of learning curve with casting to This would make it compatible with Go 1 - code that does PS. |
@tandr I think you are referring to https://golang.org/doc/faq#nil_error . That is already a subtle distinction that confuses people new to Go. I would be concerned that treating |
Thanks Ian, this is exactly it. Not having that "subtleness", or at least be able to distinguish and check for these cases would save me some of my now grey hair, it is unbelievable maddening behaviour. We have a code base that people come in and out ("oh, just add it to that component, Go is easy enough to pick up"). Every time when I review the code, I have to double check for this too. But I am not a compiler, I might miss it again. |
I agree that it would be nice to smooth out that bump in Go understanding. There are some proposals to address it, such as my #22729. But I do not think that making the '?' proposed here be different from |
I think |
Just for the record: If func E(err error) bool {
return err != nil
} |
Replacing -- for @golang/proposal-review |
I think it feels like tear in the reading flow because we are just not used to it. And to nitpick - anything with just one letter is confusing without a context :) Thanks for listening though. I am still of the opinion we can use |
@tandr Well, that proposal is not this proposal. It also at first glance seems quite similar to the rejected #32437. |
Thank Ian. In a context of just checking for error - yes, you are absolutely right, it does cover same grounds. (I guess my head is keep going in circles, wanting an operator that could be used for so many checks - pointers (naked and typed), errors, return values that contains an error - that feels like I am getting a bit confused here. I need to switch gears, thanks for the reminder). |
Linking https://github.com/tc39/proposal-optional-chaining as they have some discussion on it too. (I am starting to think there is an information field in the universe, and we are all connected. I have not seen or read that paper before commenting in all these tickets 2 months ago. This is just creepy. Or we all follow what C# does :) ) |
Hi, this proposal is somewhat related to current error handling proposal debate.
I think many people want to keep current explicit error handling,
but many other people don't like boilerplate.
If we care one aspect, another aspect is sacrificed.
So I thought simple change everyone can understand,
not touching any other thing but reduce current boilerplate a bit.
How about introduce a new symbol
?
to check the pointer is nil or non-nil?I mean we usually code like this.
with this proposal we can code like this.
I know this doesn't reduce boilerplate much. (6 characters) But personally
!= nil
is quite hard to type. And pointers are everywhere that means we type!= nil
repeatedly. It's tiresome a little.Currently we don't check a bool variable explicitly in if statement.
I mean we don't do
but do
With this proposal, we will not use
but instead use
and nil check will be
This small saving of space can be used for one-liner error handling. (I know gofmt don't permit this for now)
I don't want arguing that this is better than other proposals, but seems interesting so I want to share.
Thank you.
The text was updated successfully, but these errors were encountered: