Re: C# Generic Constraint Improve Way (Jethro Guo)



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
.



Relevant Pages

  • RE: Generics in .NET 2.0 SUCK
    ... The reason Generics in .NET 2.0 SUCK? ... when you specify a type parameter without ... probably there is a way to specify a method constraint for T ... > My second thought was that there might be some interface like IArithmetic. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Anonymous Method/Properties etc with
    ... >> an interface and then constraint T using ... > declaring the type parameter is marked as private or internal since ... > the scope of these calls are low enough that the compiler will know for sure ... If you want a dynamically typed language, use one - but I don't believe ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Anonymous Method/Properties etc with
    ... > an interface and then constraint T using ... I still think that the compiler should be smart enough not requiring any ... declaring the type parameter is marked as private or internal since ... the scope of these calls are low enough that the compiler will know for sure ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Instantiating new objects with Generics
    ... "'MyOwnClasses.MyOwnClass' must have a public parameterless instance constructor to satisfy the 'New' constraint for type parameter 'T'." ... Dim l_factory As MyOwnClasses.Factory_ ... I'm not too sure how to get round this, as it seems that it is looking for a public New constructor which I don't want to supply because I want the factory to be doing all the newing ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Value / primitive type constraints on generics
    ... > What I think he meant to ask was will the constraint using "struct" ... > allow an int to be passed for the type parameter T. ...
    (microsoft.public.dotnet.languages.csharp)