Re: feature question
From: Stephen Martin (smartin_at_removethis.emsoft.andthis.ca)
Date: 02/10/04
- Next message: nick: "Concurrency violation - concurrency_error_watch.xls (0/1)"
- Previous message: Dan Kjaergaard: "Re: iterating and filtering files using C#"
- In reply to: Kamen Yotov: "feature question"
- Next in thread: Kamen Yotov: "Re: feature question"
- Reply: Kamen Yotov: "Re: feature question"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 10 Feb 2004 05:53:49 -0500
While the return type doesn't present a problem since type D is always also
type C, the input parameter is a problem. Your delegate DF is a guarantee
that you can call that method with a parameter of type B or any subclass of
B.
If I have type Q -> B and you pass me a delegate d of type DF but it can
only accept a parameter of type A then it will compile fine but when I try
to invoke the delegate with a parameter of type Q I will get a nasty runtime
error because you've broken the contract that I can use any subclass of B
with that delegate.
There is also a similar problem with overrides. The base class guarantees
that the method can be called with any subclass of the parameter type but
the override narrows that guarantee so the method can no longer be used
polymorphically.
I think that overriding the return types is simpler since it doesn't, in
fact, violate the interface contract and I'm sure overriding the parameter
types could be done but I'm not sure it would be desirable and I don't think
it would be simple.
"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: nick: "Concurrency violation - concurrency_error_watch.xls (0/1)"
- Previous message: Dan Kjaergaard: "Re: iterating and filtering files using C#"
- In reply to: Kamen Yotov: "feature question"
- Next in thread: Kamen Yotov: "Re: feature question"
- Reply: Kamen Yotov: "Re: feature question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|