feature question
From: Kamen Yotov (kamen_at_yotov.org)
Date: 02/10/04
- Next message: Champika Nirosh: "Re: Can't see my icon... Shaped Form."
- Previous message: Sami Vaaraniemi: "Re: Nant - Nunit2 configuration"
- Next in thread: Bruno Jouhier [MVP]: "Re: feature question"
- Reply: Bruno Jouhier [MVP]: "Re: feature question"
- Reply: Stephen Martin: "Re: feature question"
- Reply: Eric Gunnerson [MS]: "Re: feature question"
- Messages sorted by: [ date ] [ thread ]
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));
}
}
- Next message: Champika Nirosh: "Re: Can't see my icon... Shaped Form."
- Previous message: Sami Vaaraniemi: "Re: Nant - Nunit2 configuration"
- Next in thread: Bruno Jouhier [MVP]: "Re: feature question"
- Reply: Bruno Jouhier [MVP]: "Re: feature question"
- Reply: Stephen Martin: "Re: feature question"
- Reply: Eric Gunnerson [MS]: "Re: feature question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|