-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Voi-92] Basic type casting via match statements (#32)
* WIP * Handle default * Basic match type checking (For now) * Assembler match support? * Almost * IT WORKS * Add Custom RTT logic to fix extension testing * Better implementation of last commit * Add tests * Update docs * Switch to : for match cases * Update docs * Update tests
- Loading branch information
Showing
27 changed files
with
788 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Control Flow | ||
|
||
## Match | ||
|
||
Used to narrow types. Can operate on object to narrow | ||
child objects or on unions to narrow union members | ||
|
||
Signature(s): | ||
``` | ||
fn match<T extends Object, U>(val: T, body: MatchBlock) -> U | ||
fn match<T extends Object, U>(val: T, bind_identifier: Identifier, body: MatchBlock) -> U | ||
``` | ||
|
||
Example: | ||
```void | ||
obj Optional | ||
obj None extends Optional | ||
obj Some extends Optional { | ||
value: i32 | ||
} | ||
fn divide(a: i32, b: i32) -> Optional | ||
if b == 0 | ||
None {} | ||
else: | ||
Some { value: a / b } | ||
fn main(a: i32, b: i32) -> String | ||
let x = a.divide(b) | ||
match(x) | ||
Some: "The value is ${x}" | ||
None: "Error: divide by zero" | ||
else: "Bleh" | ||
``` | ||
|
||
The second signature of match is useful when the value being matched against | ||
is not already bound to an identifier (i.e. dot pipelines): | ||
```void | ||
fn main(a: i32, b: i32) -> String | ||
a.divide(b) | ||
.match(x) // Here, match binds the result of the previous expression to x | ||
Some: "The value is ${x}" | ||
None: "Error: divide by zero" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.