Skip to content

How to "unwrap" union types? #36936

Discussion options

You must be logged in to vote

Hi @tutiplain,
Math operations are not allowed with error types. So you have to ensure that the variable myFloat doesn't contain an error value before using it for math operations. IMO you can do it using either type narrowing or checking expressions.

float|error myFloat = 12;
    
if myFloat !is error { // using type narrowing
    myFloat = myFloat * 12;
}
public function main() returns error? {
    float|error myFloat = 12;
    myFloat = 12 * (check myFloat); // using check-expr
}

Does this solve your problem?

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@SandaruJayawardana
Comment options

@tutiplain
Comment options

Answer selected by tutiplain
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants