feature question

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

From: Kamen Yotov (kamen_at_yotov.org)
Date: 02/10/04


Date: Tue, 10 Feb 2004 02:12:32 -0500

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));
}
}



Relevant Pages

  • Re: President Lao PDR urges use of French to maintain peace
    ... "President Choummaly explained that language can be used as a tool to ... which is why every delegate should ... then you know why those 256 delegates from 64 countries and seven ... to what they suppose to do in vientiane. ...
    (soc.culture.laos)
  • Re: C#3 and/or C# 2008
    ... The 2008 versions of Visual Studio are the ones that included the C# compiler supporting version 3 of the language. ... But "version 3" applies only to the language itself, not any Visual Studio product. ... Some specific delegate types included in some specific versions of .NET? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: feature question
    ... If I have type Q -> B and you pass me a delegate d of type DF but it can ... violate the interface contract and I'm sure overriding the parameter ... > I would argue that from formal language type theory, ... > The new auxF is compatible with the delegate and does exactly what we ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: feature question
    ... This is usually known as "delegate contravariance" (assuming I've got my co ... > I would argue that from formal language type theory, ... > The new auxF is compatible with the delegate and does exactly what we ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: feature question
    ... Your delegate DF is a guarantee ... > error because you've broken the contract that I can use any subclass of B ... violate the interface contract and I'm sure overriding the parameter ... >> don't think that it adds any further complexity in the language and it ...
    (microsoft.public.dotnet.languages.csharp)