And the same thing in D:
struct Square
{
int sideLength;
}
struct Rectangle {
int length;
int width;
}
import std.variant;
alias Shape = Algebraic!(Square, Rectangle);
import std.typecons;
auto to_shape(Tuple!(int, int) shape) pure nothrow
{
if (shape[0] == shape[1]) return Shape(Square(shape[0]));
else return Shape(Rectangle(shape.expand));
}
void main() {
auto unknowns = [
tuple(5, 4),
tuple(3, 2),
tuple(4, 4)
];
import std.stdio;
import std.algorithm;
writeln(unknowns.map!(to_shape));
}
[Rectangle(5, 4), Rectangle(3, 2), Square(4)]