Do functional generics exist and what is the correct name for them if they do?
- by voroninp
Consider the following generic class:
public class EntityChangeInfo<EntityType,TEntityKey>
{
ChangeTypeEnum ChangeType {get;}
TEntityKeyType EntityKey {get;}
}
Here EntityType unambiguously defines TEntityKeyType.
So it would be nice to have some kind of types' map:
public class EntityChangeInfo<EntityType,TEntityKey> with map
< [ EntityType : Person -> TEntityKeyType : int]
[ EntityType : Car -> TEntityKeyType : CarIdType ]>
{
ChangeTypeEnum ChangeType {get;}
TEntityKeyType EntityKey {get;}
}
Another one example is:
public class Foo<TIn> with map
< [TIn : Person -> TOut1 : string, TOut2 : int, ..., TOutN : double ]
[TIn : Car -> TOut1 : int, TOut2 :int, ..., TOutN : Price ] >
{
TOut1 Prop1 {get;set;}
TOut2 Prop2 {get;set;}
...
TOutN PropN {get;set;}
}
The reasonable question: how can this be interpreted by the compiler?
Well, for me it is just the shortcut for two structurally similar classes:
public sealed class Foo<Person>
{
string Prop1 {get;set;}
int Prop2 {get;set;}
...
double PropN {get;set;}
}
public sealed class Foo<Car>
{
int Prop1 {get;set;}
int Prop2 {get;set;}
...
Price PropN {get;set;}
}
But besides this we could imaging some update of the Foo<>:
public class Foo<TIn> with map
< [TIn : Person -> TOut1 : string, TOut2 : int, ..., TOutN : double ]
[TIn : Car -> TOut1 : int, TOut2 :int, ..., TOutN : Price ] >
{
TOut1 Prop1 {get;set;}
TOut2 Prop2 {get;set;}
...
TOutN PropN {get;set;}
public override string ToString()
{
return string.Format("prop1={0}, prop2={1},...propN={N-1},
Prop1, Prop2,...,PropN);
}
}
This all can seem quite superficial but the idea came when I was designing the messages for our system. The very first class. Many messages with the same structure should be discriminated by the EntityType.
So the question is whether such construct exists in any programming language?