Created
July 10, 2020 08:16
-
-
Save geoff-m/a28949acbfd71af1436fb4a76cb0cc96 to your computer and use it in GitHub Desktop.
Demo of compilation of stupid code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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