-- tests/interface.cat -- interfaces: structural typing at runtime. -- -- `interface` declares a named contract; a class may declare -- `implements I` (checked at class definition) and any object answers -- the structural `is` operator, so a duck-typed object satisfies an -- interface too. interface Drawable function draw() print("default draw") end end interface Shape extends Drawable function area() return 0 end end class Circle implements Shape function draw() print("draw circle") end function area() return 3 end end local c = Circle() print("circle is Drawable:", c is Drawable) print("circle is Shape:", c is Shape) c.draw() print("circle area:", c.area()) -- a duck-typed object (no `implements`) still satisfies structurally class Blob function draw() print("blob draw") end function area() return 1 end end local b = Blob() print("blob is Drawable:", b is Drawable) print("blob is Shape:", b is Shape) -- an object missing a required method does not satisfy class Line function draw() print("line draw") end end local l = Line() print("line is Drawable:", l is Drawable) print("line is Shape:", l is Shape) print("implements(c, Shape):", implements(c, Shape)) print("implements(l, Shape):", implements(l, Shape))