redefinition of generic methods

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



it is obvious that you cannot have two [or more] methods that differ only on returning value:

void F() { }
int F() { }

it is however interesting (and to me : surprizing) that this restriction also applies to generic methods that differ on constraints:

T F<T>() where T : class { }
T F<T>() where T : struct { }

note that constraints are strictly exclusive here. it would be deterministic then for the compiler to pick a method to call:

string s = F<string>(); // pick first F
int i = F<int>(); // pick second F

I do not think that this is a compiler-only issue, I belive it is rather caused by IL limitations. However, since .NET constraints are significantly limited, I belive it would be easy to determine if constraints are exclusive or not in general case [Am I wrong?].

Wiktor Zychla

.



Relevant Pages

  • Re: lsqcurvefit and bounds
    ... that the constraints don't matter. ... But the results differ. ... When I wanted to compare the results to an unconstrained model I ...
    (comp.soft-sys.matlab)
  • Re: alias for Integer
    ... On 18/11/2011 9:50 AM, jrobinss wrote: ... The advantage is that the code is auto-documented, but even better that the compiler will check that I never get mixed up in different indexes, ... public final int value; ... Do they differ in upper and lower bounds, ...
    (comp.lang.java.programmer)
  • Re: Local Class Question
    ... > public static Foo getFooImpl(final int i) { ... The only way I can see this as working is that the compiler ... ever differ from the original. ... method is distinct from the copy in the inner class instance, however, ...
    (comp.lang.java.programmer)
  • Re: Constraints on values, promises to your compiler, pros cons
    ... static pre-/post-conditions and invariants and #2 dynamic constraints. ... pro: enables easy specializations ... I suppose it's not such a great example, because the compiler could do this automatically, but then we'd have to teach the compiler when to make a specialization and when not to. ... constrained index which range is from the argument array. ...
    (comp.lang.misc)
  • Re: Pure and internal procedures
    ... The idea of a pure procedure is: The compiler knows what effects a procedure has simply by knowing its interface. ... actually mandates that the processor *must check* that the programmer is ... It does that through a myriad of constraints. ... that there were requirements on pure procedures that were not ...
    (comp.lang.fortran)