Last active
November 1, 2017 16:55
-
-
Save michaelfairley/5f80a7efc1c19ddc1b247590b805622f 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
type Bid = Int where self >= 1 && self <= 7; | |
fn doubled(bid: Bid) -> Int { | |
bid * 2 | |
} | |
// The return type of ^^^ could be `Int where self >= 2 && self <= 14`, but since | |
// Int is a supertype of that, just returning `Int` is fine | |
fn main() { | |
n = get_console_input.to_int().or_go_boom() | |
if n < 1 && n > 7 { | |
print("A bid's gotta be between 1 and 7") | |
} else { | |
print(doubled(n)) | |
} | |
} | |
fn bad_main() { | |
n = get_console_input().to_int().or_go_boom() | |
print(doubled(n)) | |
// ^^^ Compile error: N is an `Int`, but double wants a `Bid` (`Int where self >= 1 && self <= 7`) | |
// OR | |
// pass --check-things-at-runtime-instead-of-compile-time | |
// or --dont-bother-checking-my-types-my-code-is-perfect | |
// to get this running while prototyping | |
} | |
fn assert_main() { | |
n = get_console_input.to_int().or_go_boom() | |
assert(n >= 1 && n <= 7) | |
// ^^^ will blow up at runtime if it's not true. | |
// Beforehand, n's type is `Int`, after that line, it's `Int where self >= 1 && self <= 7`. | |
// Alternately, that line could be `i_solemnly_swear(n >= 1 && n <= 7)`, which changes the type | |
// but _doesn't_ insert the runtime check. | |
print(doubled(n)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment