Interfaces in Chapel
In Chapel, interfaces are not a built-in language feature. However, we can achieve similar functionality using classes and inheritance. Here’s how we can implement the geometric shapes example:
use Math;
// Define an abstract class instead of an interface
abstract class Geometry {
proc area(): real {
halt("area() must be implemented");
}
proc perim(): real {
halt("perim() must be implemented");
}
}
// Implement the Geometry class for rectangle
class Rect: Geometry {
var width, height: real;
proc init(width: real, height: real) {
this.width = width;
this.height = height;
}
override proc area(): real {
return width * height;
}
override proc perim(): real {
return 2 * width + 2 * height;
}
}
// Implement the Geometry class for circle
class Circle: Geometry {
var radius: real;
proc init(radius: real) {
this.radius = radius;
}
override proc area(): real {
return Math.pi * radius * radius;
}
override proc perim(): real {
return 2 * Math.pi * radius;
}
}
// A generic measure function that works on any Geometry object
proc measure(g: Geometry) {
writeln(g);
writeln(g.area());
writeln(g.perim());
}
// Main function
proc main() {
var r = new Rect(3, 4);
var c = new Circle(5);
measure(r);
measure(c);
delete r;
delete c;
}In this Chapel implementation:
We define an abstract
Geometryclass instead of an interface. This class declares thearea()andperim()methods that must be implemented by subclasses.We create
RectandCircleclasses that inherit fromGeometryand provide their own implementations ofarea()andperim().The
measurefunction takes aGeometryobject as an argument, allowing it to work with any subclass ofGeometry.In the
mainfunction, we create instances ofRectandCircle, and pass them to themeasurefunction.Chapel uses explicit memory management, so we need to delete the objects we created with
new.
To run this program, save it as geometry.chpl and use the Chapel compiler:
$ chpl geometry.chpl -o geometry
$ ./geometryThis will output something similar to:
{width = 3.0, height = 4.0}
12.0
14.0
{radius = 5.0}
78.5398163397448
31.4159265358979While Chapel doesn’t have interfaces in the same way as some other languages, this approach using abstract classes and inheritance provides similar functionality for defining common behavior across different types.