You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Python there is no way to handle an error in an expression, this is also the case for match and non inline if statements. This becomes a problem when you want to assign a variable directly with the result of an if, try or match statement.
Mojo could add a way to create an expression out of a group of statements.
Zig has something similar and Rust too, but usually you don't need them because most things in Rust are expressions and not statements.
# with without anything becomes a way to turn a block into an expressionvarresult=with:
try:
something()
break"success"exceptValueError:
break"ValueError"exceptIndexError:
break"IndexError"# same code without this featurevarresult: String
try:
something()
result ="success"exceptValueError:
result ="ValueError"exceptIndexError:
result ="IndexError"
Same thing in Rust:
let result = matchsomething(){Ok(_) => "success".to_string(),Err(e) => match e {"ValueError" | "IndexError" => "".to_string(),
_ => "other error".to_string(),},};
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
In Python there is no way to handle an error in an expression, this is also the case for
match
and non inlineif
statements. This becomes a problem when you want to assign a variable directly with the result of anif
,try
ormatch
statement.Mojo could add a way to create an expression out of a group of statements.
Zig has something similar and Rust too, but usually you don't need them because most things in Rust are expressions and not statements.
Same thing in Rust:
Beta Was this translation helpful? Give feedback.
All reactions