Re: feature question
From: Bruno Jouhier [MVP] (bjouhier_at_club-internet.fr)
Date: 02/10/04
- Next message: Susanne Christe: "Registeresd JIT debugger is not avaialble"
- Previous message: Luca Paganelli: "Re: Post Request and C#"
- In reply to: Bruno Jouhier [MVP]: "Re: feature question"
- Next in thread: Daniel O'Connell [C# MVP]: "Re: feature question"
- Reply: Daniel O'Connell [C# MVP]: "Re: feature question"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 10 Feb 2004 09:06:00 +0100
Ooop, my post is not very clear. You should read:
1) a class relationships A -> B and C -> D
2) a base method BM with signature "C M(B b);"
3) an override OM with signature "D M(A a);"
Typical example is the Object/Factory pattern where you have two parallel
hierarchies: BaseObject, DerivedObject on one side and BaseFactory,
DerivedFactory on the other side. You want to define
BaseFactory GetFactory() in the BaseObject class
and override it as:
DerivedFactory GetFactory() in the DerivedObject class
There are ways around it (with the "new" keyword) but things would be
cleaner if the type system was a bit smarter.
Note: GetFactory() can be replaced by a get-only Factory property, this does
not change the point above. On the other hand, we cannot make the property
get/set because then we have a typing problem on the set (the override set
accessor has a more restrictive signature than the base accessor).
Bruno
"Bruno Jouhier [MVP]" <bjouhier@club-internet.fr> a écrit dans le message de
news:%23%23F5po67DHA.2480@TK2MSFTNGP10.phx.gbl...
> Makes sense. This does not create any hole in the type system and it could
> be handy in some cases.
>
> The same reasoning could be applied to overrides:
>
> 1) a class relationships A -> B and C -> D
> 2) a base method with signature "C BM(B b);"
> 3) an override with signature "D OM(A a);"
>
> Why doesn't C# let you override BM with OM in this case? Quite often, I
> would like to do this, especially replacing the return type (C) by a more
> specialized one (D) into the override, but I cannot do it, I am forced to
> use C as return type and then my code is polluted by (D) casts.
>
> To me, this is more rigorous than the following:
>
> A[] aArray = new B[10];
>
> which will let you store objects that don't derive from B into aArray. The
> compiler allows it and you get a runtime exception!
>
> Bruno.
>
> "Kamen Yotov" <kamen@yotov.org> a écrit dans le message de
> 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: Susanne Christe: "Registeresd JIT debugger is not avaialble"
- Previous message: Luca Paganelli: "Re: Post Request and C#"
- In reply to: Bruno Jouhier [MVP]: "Re: feature question"
- Next in thread: Daniel O'Connell [C# MVP]: "Re: feature question"
- Reply: Daniel O'Connell [C# MVP]: "Re: feature question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|