Skip to content

Instantly share code, notes, and snippets.

@coffeemug
Created July 13, 2020 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coffeemug/e27fae18cd6307eabafe977e6b02ab3b to your computer and use it in GitHub Desktop.
Save coffeemug/e27fae18cd6307eabafe977e6b02ab3b to your computer and use it in GitHub Desktop.
// 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