Early return when null

I am not sure how to write code for an early exit when null.
Suppose I have this function:

fn get_node() ?*Node
{
    // return some node pointer or null
} 

Now calling this one inside another method

fn find_something_inside_my_tree() ?*Node
{
    if (get_node())|node|
    {
        // go ahead with your node
    }
}

but I do not want that. What I want is:

fn find_something_inside_my_tree() ?*Node
{
    var node = get_node());
    if (node == null) return null;
    // go ahead with your node, but now we again have to check on null.
}

In reality the function is more complicated. There are more early exits to null.
Is there ‘the negative way’ to write this?
I hope the question is clear.

You can try using the orelse keyword like this:

fn find_something_inside_my_tree() ?*Node {
    var node = get_node() orelse return null;
    // now node is not null
}
6 Likes

Ah of course, thanks. I was confused.

See also this.