Skip to content

Instantly share code, notes, and snippets.

@jarble
Last active June 22, 2022 20:13
Show Gist options
  • Save jarble/f34cae13c2e78a3f1f1669d9b9508a33 to your computer and use it in GitHub Desktop.
Save jarble/f34cae13c2e78a3f1f1669d9b9508a33 to your computer and use it in GitHub Desktop.
An implementation of object-oriented classes in MiniZinc
% define a Rectangle "class"
var int: Rectangle(var int: width, var int: height) =
let {
var int: this;
constraint Type(this) = Rectangle; %define the "type" of the instance
%define some "instance methods"
constraint area(this) = width*height;
constraint perimeter(this) = (width+height)*2;
constraint width(this) = width;
constraint height(this) = height;
} in this;
%this enum should contain the list of class names
enum Type = {Rectangle};
function var Type: Type(var int:a);
%declare the "instance methods"
function var int: area(var int:this) = let {var int:result;} in result;
function var int: height(var int:a) = let {var int:result;} in result;
function var int: width(var int:a) = let {var int:result;} in result;
function var int: perimeter(var int:a) = let {var int:result;} in result;
%create an instance of the "class"
var int: rect = Rectangle(3,4);
var int: area1 = area(rect);
solve satisfy;
% print the area of the rectangle
output [show(area1),"\n"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment