Skip to content

Null safety/coalescing function? #1309

@midn1

Description

@midn1

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 named or, or something similar.
I believe this would make code a bit cleaner, as there would be less ifs and temporary variables scattered around the place.

Include sample usage where appropriate.

#include"json.hpp"
#include<iostream>
int main() {
    nlohmann::json j = R"({"x": 5, "y": null})"_json;
    std::cout << j["x"].or(29) << ' ' << j["y"].or(10) << std::flush;
}

Expected output would be:

 5 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    solution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions