#Checking for optional value

1 messages · Page 1 of 1 (latest)

crystal phoenix
#

Let's say I have some variable which is optional and in function. I would like to return something in function if the passing argument is null, but otherwise go with the function logic like this:

    if(foo == null) return;
    // function logic here
}

rather than:

    if(foo) |val| {
    // function logic here
    }
}

The problem with first is if I want to use another function which requires u8 then I'm getting type error. Is there any way to do with first method and don't get type errors when compiling?

upper oak
#

foo.?

#

it gets the underlying value of an optional and panics if it's null but since you guaranteed that it's not null before, it's safe