While working with a lot of string comparisons it gets tedious to write elif <variable> == "Some String". So since rust has this really nice way of doing this, using match statements. I figured it would be best to implement something similar to it.
Designing the match statement and what it can be used for
match <expression>
<expression> => do <expression> //inline way
<expression> => //multi line way
<expression>
<expression>
;
_ => do <expression> //default statement
;
//Assign based on match
//The last expression in each statement is the value output
let <variable_id> = match <expression>
<expression> => do <expression> //inline way
<expression> => //multi line way
<expression>
<expression>
;
_ => do <expression> //default statement
;
//Assign without creating
<variable_id> = match <expression>
<expression> => do <expression> //inline way
<expression> => //multi line way
<expression>
<expression>
;
_ => do <expression> //default statement
;
So when using this you can now do things like this. It's obviously more useful and readable when there's more matches
let word = "Hello"
let paired_word = match word
"Hello" => do word + " World"
_ => do "Unable to find pair"
;
println(paired_word)
let i = 5
let text = match i
> 5 => do "More than 5"
< 5 => do "Less than 5"
1 => do "1"
_ => do "Unkown"
;
println(text)
class SomeClass
pub
Object active_something = Object()
Object enabled_something = Object()
int index = 0
fn is_active => bool doremi true
fn is_enabled => bool doremi true
;
let class_obj = SomeClass()
let output = match class_obj
.is_alive() => do Some(class_obj.active_something())
.is_enabled() => do Some(class_obj.enabled_something())
.index < 0 => do None<Object>()
_ =>
println("Unhandled case")
None<Object>()
;
;
Comments (0)