Skip to content

Instantly share code, notes, and snippets.

@gdejohn
Last active May 16, 2016 08:54
Show Gist options
  • Save gdejohn/8ac17b075b01009b9d8b584133304be7 to your computer and use it in GitHub Desktop.
Save gdejohn/8ac17b075b01009b9d8b584133304be7 to your computer and use it in GitHub Desktop.
Ceylon solution to the expression problem.
"The datatype."
shared interface Expression {
"The initial operation."
shared formal Integer evaluate();
}
"The initial case of the datatype."
shared interface Literal satisfies Expression {
"The value of the literal expression."
shared formal Integer literal;
evaluate() => literal;
}
"The new case of the datatype."
shared interface Addition satisfies Expression {
"Left operand."
shared formal Expression left;
"Right operand."
shared formal Expression right;
evaluate() => left.evaluate() + right.evaluate();
}
"Extension of the datatype."
shared interface Show satisfies Expression {
"The new operation."
shared formal String show();
}
"Implementation of new operation for literals."
shared interface LiteralShow satisfies Literal & Show {
show() => literal.string;
}
"Implementation of new operation for addition."
shared interface AdditionShow satisfies Addition & Show {
"Covariant refinement of inherited attribute."
shared actual formal Show left;
see(`value left`)
shared actual formal Show right;
show() => "(``left.show()`` + ``right.show()``)";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment