Created
May 6, 2017 13:43
-
-
Save matthewhammer/4e3e576edea5cea2c425d0acf95417df to your computer and use it in GitHub Desktop.
Adapton avoids 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
#[macro_use] | |
extern crate adapton; | |
use adapton::macros::*; | |
use adapton::engine::*; | |
fn main() { | |
manage::init_dcg(); | |
let nom = cell!(42); | |
let den = cell!(2); | |
// In Rust, cloning is explicit: | |
let den2 = den.clone(); // here, we clone the _global reference_ to the cell. | |
let den3 = den.clone(); // here, we clone the _global reference_ to the cell, again. | |
let div = thunk![ get!(nom) / get!(den) ]; | |
let check = thunk![ if get!(den2) == 0 { None } else { Some(get!(div)) } ]; | |
assert_eq!(get!(check), Some(21)); | |
set(&den3, 0); assert_eq!(get!(check), None); | |
set(&den3, 2); assert_eq!(get!(check), Some(21)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment