I doubt this is possible but what I would like to do is have an argument’s type depend on the value of another argument. Because it is statically defined most of the time it should work when it can(in my case it will always be used statically).
e.g.,
suppose we had a function myFunc who’s first argument was an enum of type myEnum.
myFunc(myEnum.SomeValue);
But now suppose we wanted the second argument’s type to depend on the value of the first.
So
myFunc(myEnum.Value1, x)
myFunc(myEnum.Value2, y)
in this case x would have a type that is related to Value1 and y to Value2.
It should be perfectly valid for the compiler to directly determine the type of the 2nd argument if we could specify a map. We might need a new type to handle it but as long as the first argument is a compile time constant there should be no problem with it. One can use polymorphism to handle the various cases too or, in my case, using a factory.
The only type the compiler cannot deduce the type of the 2nd argument is when the first is not a compile time constant in which case a run time error can be thrown if the 2nd argument is not of the proper time.
The main point of doing this is to break a large enum into smaller ones that form a hierarchy. One could simply have a single large enum containing all the combinations but it gets out of control very quickly.
I seriously doubt C# supports such a feature but maybe there is a possible way to get it to work(using attributes or whatever)?
Example of behavior
public enum Value1Types { Type1, Type2 }
public enum Value2Types { TypeA, TypeB }
public class FakeEnum
{
public class EnumTypes
{
public abstract class BaseEnum { public abstract int Value { get; } }
public class Value1 : BaseEnum { public override int Value { get { return 1; } } }
public class Value2 : BaseEnum { public override int Value { get { return 2; } } }
}
public static class EnumValues
{
// Singletons
public static readonly EnumTypes.Value1 Value1 = new EnumTypes.Value1();
public static readonly EnumTypes.Value2 Value2 = new EnumTypes.Value2();
}
public void AcceptValue(EnumTypes.Value1 typeController, Value1Types s) { Console.WriteLine("Value1 " + s.ToString()); }
public void AcceptValue(EnumTypes.Value2 typeController, Value2Types s) { Console.WriteLine("Value2 " + s.ToString()); }
public void Test()
{
// Notice how the same method's second arugment's type depends on the value of the first
AcceptValue(EnumValues.Value1, Value1Types.Type1); // valid
AcceptValue(EnumValues.Value2, Value2Types.TypeA); // valid
//AcceptValue(EnumValues.Value1, Value2Types.TypeB); // Invalid, compile error
}
}
If you can replace the enums with explicit types you can make something like this.
Personally I think it is to much work to get the last bit of compile time checking.