Skip to content

Instantly share code, notes, and snippets.

@geoff-m
Created July 10, 2020 08:16
Show Gist options
  • Save geoff-m/a28949acbfd71af1436fb4a76cb0cc96 to your computer and use it in GitHub Desktop.
Save geoff-m/a28949acbfd71af1436fb4a76cb0cc96 to your computer and use it in GitHub Desktop.
Demo of compilation of stupid code
using System;
namespace test
{
class PureObjectOrientedWorld
{
static void Main(string[] args)
{
var p = new PureObjectOrientedWorld();
p.Test(5, 12, 12);
p.Test(13, 6, 13);
}
void Test(int a, int b, int expectedResult)
{
var actual = max(a, b);
if (actual == expectedResult)
Console.WriteLine("Test passed.");
else
Console.WriteLine("Test failed.");
}
// "pure object-oriented world"
public int max(int a, int b)
{
return new If(
new GreaterThan(a, b),
a, b
);
}
interface IComparison
{
bool Evaluate();
}
public class GreaterThan : IComparison
{
public GreaterThan(int left, int right)
{
Result = left > right;
}
public readonly bool Result;
bool IComparison.Evaluate()
{
return Result;
}
}
class If
{
public If(IComparison c, int trueResult, int falseResult)
{
if (c.Evaluate())
Result = trueResult;
else
Result = falseResult;
}
public readonly int Result;
public static implicit operator int(If i)
{
return i.Result;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment