Created
July 13, 2020 04:35
-
-
Save coffeemug/e27fae18cd6307eabafe977e6b02ab3b to your computer and use it in GitHub Desktop.
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
// Got implicit conversion working the compiler! | |
// You implement it like this: | |
enum maybe<T> = none | some(T); | |
// `T?` is just sugar for `maybe<T>` | |
impl<T> T? { | |
// the colon prefix means function is static | |
fn :from(x: T) -> T? { | |
some(x); | |
} | |
} | |
// here the compiler automagically calls `i32?::from` | |
let x : i32? = 5; | |
// Implicit conversion turns code like this: | |
let root = insert(none, 10); | |
root = insert(some(root), 5); | |
root = insert(some(root), 7); | |
root = insert(some(root), 15); | |
root = insert(some(root), 13); | |
dfs(some(root)); | |
// Into code like this: | |
let root = insert(none, 10); | |
root = insert(root, 5); | |
root = insert(root, 7); | |
root = insert(root, 15); | |
root = insert(root, 13); | |
dfs(root); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment