-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Null safety/coalescing function? #1309
Comments
For objects, we have a |
Here is the documentation of the value function. I think the proposed code is a bit dangerous: What should happen in case of
That's why I am not convinced that a member function |
I'm not writing this to be annoying. I fully agree with you on following the std::map's behavior of j["not-a-key"], from the json_pointer thread... But, if the j["not-a-key"] did not create a key/nullvalue but generated a deferred key-reference class, (which would then set the key on operator= or throw an exception on operator T), this problem of .or would also go away, as the deferred key could, as well, have the .or function. |
We implement the |
I don't like your "magic or() function approach", regardless that If you insist of having a function that "gets a value or return a default value", I'd name it this way:
Why? Well, always remember: You don't write the code for yourself! If your project is getting successfull, there will be another guy who might be your successor or a maintainer who is hunting for a bug. And – obviously, because you are the best C++ hacker on earth – their C++ knowledge is far less than yours, but they also have to understand your code, even at 3 a.m.! So I'd always prefer functions with clear names instead of magic syntax or awkward overloaded operators. :-) |
@nlohmann : Excellent! Okay, I would have chosen another name, because |
@lvivtotoro Can I close this issue in favor of the |
Yup. |
Describe the feature in as much detail as possible.
A null coalescing function, similar to that of the null coalescing operator found in most modern languages (
??
in C#,?:
in Kotlin,||
in JS almost). Except in here it would be a template function namedor
, or something similar.I believe this would make code a bit cleaner, as there would be less
if
s and temporary variables scattered around the place.Include sample usage where appropriate.
Expected output would be:
The text was updated successfully, but these errors were encountered: