aboutsummaryrefslogtreecommitdiff
path: root/tests/pos/t0031.scala
diff options
context:
space:
mode:
Diffstat (limited to 'tests/pos/t0031.scala')
-rw-r--r--tests/pos/t0031.scala29
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/pos/t0031.scala b/tests/pos/t0031.scala
new file mode 100644
index 000000000..d4050c818
--- /dev/null
+++ b/tests/pos/t0031.scala
@@ -0,0 +1,29 @@
+object Main {
+
+ trait Ensure[a] {
+ def ensure(postcondition: a => Boolean): a
+ }
+
+ def require[a](precondition: => Boolean)(command: => a): Ensure[a] =
+ if (precondition)
+ new Ensure[a] {
+ def ensure(postcondition: a => Boolean): a = {
+ val result = command;
+ if (postcondition(result)) result
+ else sys.error("Assertion error")
+ }
+ }
+ else
+ sys.error("Assertion error");
+
+ def arb[a](s: List[a]) =
+ require (! s.isEmpty) {
+ s.head
+ } ensure (result => s contains result);
+
+ def main(args: Array[String]) = {
+ val s = List(1, 2);
+ Console.println(arb(s))
+ }
+
+}