Re: C# Generic Constraint Improve Way (Jethro Guo)
- From: Lasse Vågsæther Karlsen <lasse@xxxxxxxxxxx>
- Date: Tue, 13 Dec 2005 13:27:22 +0100
Jesse McGrew wrote:
Your proposed syntax is interesting. I find it confusing that the constraint class TRef is automatically matched up with the type parameter TRef without a where clause, though. Type parameter names are normally local to the generic class/method where they're declared - one class's TKey might have nothing in common with another class's TKey.
I've been thinking about possible extensions too. I'm so frustrated by the power that generics *almost* have that I might even try to hack these into Mono's compiler to test them. Here are a couple of my ideas...
Signature constraints:
public T Sum(params T[] items) where T has static T operator +(T left, T right) { T result; foreach (T item in items) result = result + item; return result; }
Other possible signature constraints:
where T has new(String session) where T has static T Parse(String value)
the new(String) constraint would be irksome as AFAIK it would match any constructor that takes a string, not necessarily one that is named "session" or has the same meaning. This is clearly what interfaces are about, documented contract, but then interfaces doesn't allow you to construct new instances of any type of object.
I've needed the first of those two and had to hack it with the Activator object. Doable but doesn't seem in the spirit of generics.
<snip>
-- Lasse Vågsæther Karlsen http://usinglvkblog.blogspot.com/ mailto:lasse@xxxxxxxxxxx PGP KeyID: 0x2A42A1C2 .
- Follow-Ups:
- Re: C# Generic Constraint Improve Way (Jethro Guo)
- From: Jesse McGrew
- Re: C# Generic Constraint Improve Way (Jethro Guo)
- References:
- C# Generic Constraint Improve Way (Jethro Guo)
- From: Jethro Guo
- Re: C# Generic Constraint Improve Way (Jethro Guo)
- From: Jesse McGrew
- C# Generic Constraint Improve Way (Jethro Guo)
- Prev by Date: Re: How to extract words out from a string?
- Next by Date: virtual properties?
- Previous by thread: Re: C# Generic Constraint Improve Way (Jethro Guo)
- Next by thread: Re: C# Generic Constraint Improve Way (Jethro Guo)
- Index(es):
Relevant Pages
|