Do functional generics exist or what is the correct name for them if they do?
Posted
by
voroninp
on Programmers
See other posts from Programmers
or by voroninp
Published on 2012-11-15T22:22:24Z
Indexed on
2012/11/15
23:26 UTC
Read the original article
Hit count: 183
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 this can be interpreted by the compiler? Well, for me it is just the sortcut 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 structrue should be discriminated by the EntityType.
So the question is whether such construct exist in any programming language?
© Programmers or respective owner