Skip to content

Instantly share code, notes, and snippets.

@michaelfairley
Created December 2, 2012 01:32
Show Gist options
  • Save michaelfairley/4186441 to your computer and use it in GitHub Desktop.
Save michaelfairley/4186441 to your computer and use it in GitHub Desktop.
Precondition enforcing type system
def sqrt(i: Int >= 0): Int >= 0 {
...
}
def good_input() {
i = Int.parse(stdio.read_line)
if i >= 0 {
print "sqrt: #{sqrt(i)}
} else {
print "Cannot take the sqrt of a negative number"
}
}
def bad_input() {
i = Int.parse(stdio.read_line)
print "sqrt: #{sqrt(i)}"
# Compiler error: sqrt takes (Int >= 0), not (Int)
}
def ok_input() {
i = Int.parse(stdio.read_line)
assert(i >= 0) # Allows the next line to compile
print "sqrt: #{sqrt(i)}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment