diff options
author | 2014-07-18 21:16:40 +0300 | |
---|---|---|
committer | 2014-07-18 21:16:40 +0300 | |
commit | 77143c833cbb14a20c704fb60fc28dd94edb44eb (patch) | |
tree | 7da7588d8dccb94fb1c5c42f23ec69c4edab2785 /Test/dafny0/Trait/TraitPolymorphism.dfy | |
parent | c377658acba5472b6d0c1e1452ce4c4c8f1fc28e (diff) |
added trait feature:
-possibility to declare traits in Dafny
-possibility to extend a class by a trait
-possibility to override body-less methods
Diffstat (limited to 'Test/dafny0/Trait/TraitPolymorphism.dfy')
-rw-r--r-- | Test/dafny0/Trait/TraitPolymorphism.dfy | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/Test/dafny0/Trait/TraitPolymorphism.dfy b/Test/dafny0/Trait/TraitPolymorphism.dfy new file mode 100644 index 00000000..b1ee9eea --- /dev/null +++ b/Test/dafny0/Trait/TraitPolymorphism.dfy @@ -0,0 +1,65 @@ +// RUN: %dafny /compile:0 /print:"%t.print" /dprint:"%t.dprint" "%s" > "%t"
+// RUN: %diff "%s.expect" "%t"
+
+trait T1
+{
+ var f: int;
+
+ function method Plus (x:int, y:int) : int
+ requires x>y;
+ {
+ x + y
+ }
+
+ function method Mul (x:int, y:int, z:int) : int
+ requires x>y;
+ {
+ x * y * z
+ }
+
+ //function method BodyLess1() : int
+
+ static method GetPhoneNumber (code:int, n:int) returns (z:int)
+ {
+ z := code + n;
+ }
+
+ method TestPhone ()
+ {
+ var num : int;
+ num := GetPhoneNumber (10, 30028);
+ }
+}
+
+trait T2
+{
+}
+
+class C1 extends T1
+{
+ method P2(x:int, y:int) returns (z:int)
+ requires x>y;
+ {
+ z:= Plus(x,y) + Mul (x,y,1);
+ }
+}
+
+
+
+method Good(c: C1) returns (t: T1)
+ensures c == t;
+{
+ t := c;
+}
+
+method Bad1(c: C1) returns (t: T2)
+ensures c == t;
+{
+ t := c; //error, C1 has not implemented T2
+}
+
+method Bad2(c: C1) returns (t: T1)
+ensures c == t;
+{
+ c := t; //error, can not assign a trait to a class
+}
|