Created
December 2, 2012 01:32
-
-
Save michaelfairley/4186441 to your computer and use it in GitHub Desktop.
Precondition enforcing type system
This file contains hidden or 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
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