Skip to content

Instantly share code, notes, and snippets.

@mathfarmer
Last active September 9, 2016 18:03
Show Gist options
  • Save mathfarmer/8028747bfb12414914ee7b29392c91eb to your computer and use it in GitHub Desktop.
Save mathfarmer/8028747bfb12414914ee7b29392c91eb to your computer and use it in GitHub Desktop.
When your generic type parameters start reminding you of this, you should probably reconsider your approach...
// <copyright file="JackHouse.cs">
// Copyright 2016 by Douglas A. Telfer
// SPDX-License-Identifier: MPL-2.0 or GPL-2.1 or GPL-3.0
// </copyright>
// <author>${Author}</author>
// <date>Friday, September 9, 2016</date>
// <summary></summary>
using System;
namespace TheHouseThatJackBuilt
{
public interface IReciteable
{
string RecitePart();
}
public static class ThisDeclarer
{
public static string Recite(this IReciteable reciteable)
{
return Environment.NewLine + "This is " + reciteable.RecitePart();
}
}
public class Jack : IReciteable
{
public string RecitePart()
{
return "Jack";
}
}
public class House<TBuilder> : IReciteable
where TBuilder : Jack, new()
{
public string RecitePart()
{
return "the house that " + new TBuilder().RecitePart() + " built";
}
}
public class House : House<Jack>
{
}
public class Malt<TLocation, TBuilder> : IReciteable
where TLocation : House<TBuilder>, new()
where TBuilder : Jack, new()
{
public string RecitePart()
{
return "the malt" + Environment.NewLine + "That lay in " + new TLocation().RecitePart();
}
}
public class Malt : Malt<House, Jack>
{
}
public class Rat<TFood, TLocation, TBuilder> : IReciteable
where TFood : Malt<TLocation, TBuilder>, new()
where TLocation : House<TBuilder>, new()
where TBuilder : Jack, new()
{
public string RecitePart()
{
return "the rat" + Environment.NewLine + "That ate " + new TFood().RecitePart();
}
}
public class Rat : Rat<Malt, House, Jack>
{
}
public class Cat<TVictim, TFood, TLocation, TBuilder> : IReciteable
where TVictim : Rat<TFood, TLocation, TBuilder>, new()
where TFood : Malt<TLocation, TBuilder>, new()
where TLocation : House<TBuilder>, new()
where TBuilder : Jack, new()
{
public string RecitePart()
{
return "the cat" + Environment.NewLine + "That killed " + new TVictim().RecitePart();
}
}
public class Cat : Cat<Rat, Malt, House, Jack>
{
}
public class Dog<TWorriee, TVictim, TFood, TLocation, TBuilder> : IReciteable
where TWorriee : Cat<TVictim, TFood, TLocation, TBuilder>, new()
where TVictim : Rat<TFood, TLocation, TBuilder>, new()
where TFood : Malt<TLocation, TBuilder>, new()
where TLocation : House<TBuilder>, new()
where TBuilder : Jack, new()
{
public string RecitePart()
{
return "the dog" + Environment.NewLine + "That worried " + new TWorriee().RecitePart();
}
}
public class Dog : Dog<Cat, Rat, Malt, House, Jack>
{
}
public static class Program
{
public static void Main(string[] args)
{
Console.WriteLine();
Console.WriteLine(new House().Recite());
Console.WriteLine(new Malt().Recite());
Console.WriteLine(new Rat().Recite());
Console.WriteLine(new Cat().Recite());
Console.WriteLine(new Dog().Recite());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment