Re: feature question
From: Eric Gunnerson [MS] (ericgu_at_online.microsoft.com)
Date: 02/10/04
- Next message: Martin Maat [EBL]: "Re: Initiating GUI control creation from a non-GUI thread"
- Previous message: Eric Gunnerson [MS]: "Re: defining math function at run time"
- In reply to: Kamen Yotov: "feature question"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 10 Feb 2004 14:58:27 -0800
This is usually known as "delegate contravariance" (assuming I've got my co
and contras correct today (covariance would be on the return type)), and
while it isn't supported in C# now, it may show up in the future.
-- Eric Gunnerson Visit the C# product team at http://www.csharp.net Eric's blog is at http://weblogs.asp.net/ericgu/ This posting is provided "AS IS" with no warranties, and confers no rights. "Kamen Yotov" <kamen@yotov.org> wrote in message news:eYsjZV67DHA.2952@TK2MSFTNGP09.phx.gbl... > hi all, > i first posted this on > http://msdn.microsoft.com/vcsharp/team/language/ask/default.aspx > (ask a c# language designer) a couple of days ago, but no response so far... > therefore, i am pasting it here as well... enjoy! > > (you can skip to the source at the end of the message if you like...) > > Consider: > > 1) a class relationships A -> B and C -> D > 2) a delegate declaration "delegate C DF (B b);" > 3) a function with signature "D F (A a);" > > According to C# syntax and semantics, an instance of the delegate DF > cannot be created from the function F, as the parameter types and the > return type do not match the delegate declaration. > > I would argue that from formal language type theory, it should be > possible, because of polymorphism. > > Suppose this hypothetic code: > > { > DF d = new DF(F); > > B b = new ... > > C c = d(b); > } > > I have no reasons to believe that such code should break. In fact: > > 1) d is an instance of DF using function F > > 2) d is called with parameter b which is of type B, which derives from > A, > therefore F can be called itself with b > > 3) d's result is assigned to c, which is of type C, which type D derives > from. Because F returns D, it's result is assignable to C-typed > objects... > > In summary I would argue that it is type-safe to allow this. Further I > don't think that it adds any further complexity in the language and it > is something worth having. I personally expected to see it there, was > disappointed when it I did not, and I have needed it a number of times. > > One fact that confirms my thesis is that if we write the following > function: > > C auxF (B b) > { > return F(b); > } > > The new auxF is compatible with the delegate and does exactly what we > want. > To refine my question further I would say: Why do we need auxF??? > > Please find a "working" C# program below that illustrates the issue! > > Please outline the reasons for which this is not part of the C# > language. > Do not hesitate to contact me personally if you need more information > regarding this question. > > Best Regards, > Kamen Yotov > kamen@yotov.org > http://yotov.org > > using System; > > class A {} > class B: A {} > class C {} > class D: C {} > > delegate C DF (B b); > > class Run > { > static D F (A a) > { > return new D(); > } > > static C auxF (B b) // I would like to get rid of this function > { > return F(b); > } > > static void Main(string[] args) > { > DF d = new DF(auxF); // I want "new DF(F)" here! > B b = new B(); > > Console.WriteLine(d(b)); > } > } > > > >
- Next message: Martin Maat [EBL]: "Re: Initiating GUI control creation from a non-GUI thread"
- Previous message: Eric Gunnerson [MS]: "Re: defining math function at run time"
- In reply to: Kamen Yotov: "feature question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|