digitalmars.D - Should certain abstract classes be instantiable?
- Andrei Alexandrescu (10/10) Oct 01 2009 Consider:
- Jeremie Pelletier (3/19) Oct 01 2009 What's the point of marking fun() abstract if it has an implementation,
- Andrei Alexandrescu (4/25) Oct 01 2009 It may offer incomplete functionality that is to be reused and enhanced
- dsimcha (4/29) Oct 01 2009 If you are in that situation, then don't declare the class abstract. I ...
- Andrei Alexandrescu (3/32) Oct 01 2009 They can't be instantiated.
- Lutger (4/40) Oct 01 2009 Exactly, to return to your original question: if you allow abstract clas...
- Lutger (5/32) Oct 01 2009 Then again, if it offers incomplete functionality why would you want it ...
- Jeremie Pelletier (6/38) Oct 01 2009 Exactly, I only mark members as abstract if they have no
- Jarrett Billingsley (3/12) Oct 01 2009 Uh... why?
- Andrei Alexandrescu (5/22) Oct 01 2009 Because I want to give a good argument one way or another in TDPL. FWIW,...
- Jarrett Billingsley (9/32) Oct 01 2009 h
- Andrei Alexandrescu (3/29) Oct 01 2009 It's not a no-op. Try it.
- Jarrett Billingsley (3/8) Oct 02 2009 Yeah, not *currently*, but isn't that what you're proposing?
- Andrei Alexandrescu (15/23) Oct 02 2009 No. I think it would help going back to my original message instead of
- Jarrett Billingsley (5/18) Oct 02 2009 Yes, I see now the parenthesized "requires overriding in derivees" now.
- Andrei Alexandrescu (3/25) Oct 02 2009 It was a question, you one-liner-asker you.
- Jarrett Billingsley (4/5) Oct 02 2009 What?
- Ary Borenszweig (4/24) Oct 03 2009 Umm... so it defines a body that will never be used because that class
- Andrei Alexandrescu (14/39) Oct 03 2009 import std.stdio;
- Nick Sabalausky (4/21) Oct 04 2009 Not a rhetorical or a loaded question: Has that sort of thing ever been
- Ary Borenszweig (4/30) Oct 05 2009 I was wondering the same. It's also very bug prone because when
- Lionello Lunesu (4/14) Oct 01 2009 If it were instantiable, what would be the difference between "abstract"...
- Lionello Lunesu (5/24) Oct 02 2009 OK, so I reread your post and yes, "it requires overriding in derivees."
- Steven Schveighoffer (13/22) Oct 02 2009 If you want to force a class to be abstract, even though it technically ...
- Justin Johansson (76/99) Oct 02 2009 I had a brilliant maths teacher at high school. Every now and then
- Justin Johansson (6/38) Oct 03 2009 Just in case I'm misunderstood .. I don't believe the concept is particu...
Consider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder? Andrei
Oct 01 2009
Andrei Alexandrescu wrote:Consider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder? AndreiWhat's the point of marking fun() abstract if it has an implementation, I thought the compiler disallowed that.
Oct 01 2009
Jeremie Pelletier wrote:Andrei Alexandrescu wrote:It may offer incomplete functionality that is to be reused and enhanced by descendants. AndreiConsider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder? AndreiWhat's the point of marking fun() abstract if it has an implementation, I thought the compiler disallowed that.
Oct 01 2009
== Quote from Andrei Alexandrescu (SeeWebsiteForEmail erdani.org)'s articleJeremie Pelletier wrote:If you are in that situation, then don't declare the class abstract. I thought the whole point of abstract classes was that they can't be instantiated. If it can be instantiated, then what does abstract even mean?Andrei Alexandrescu wrote:It may offer incomplete functionality that is to be reused and enhanced by descendants. AndreiConsider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder? AndreiWhat's the point of marking fun() abstract if it has an implementation, I thought the compiler disallowed that.
Oct 01 2009
dsimcha wrote:== Quote from Andrei Alexandrescu (SeeWebsiteForEmail erdani.org)'s articleThey can't be instantiated. AndreiJeremie Pelletier wrote:If you are in that situation, then don't declare the class abstract. I thought the whole point of abstract classes was that they can't be instantiated. If it can be instantiated, then what does abstract even mean?Andrei Alexandrescu wrote:It may offer incomplete functionality that is to be reused and enhanced by descendants. AndreiConsider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder? AndreiWhat's the point of marking fun() abstract if it has an implementation, I thought the compiler disallowed that.
Oct 01 2009
Andrei Alexandrescu wrote:dsimcha wrote:Exactly, to return to your original question: if you allow abstract classes to be instantiable it hinders a design where a class is mandated to be a base class only with default implementations of all methods.== Quote from Andrei Alexandrescu (SeeWebsiteForEmail erdani.org)'s articleThey can't be instantiated. AndreiJeremie Pelletier wrote:If you are in that situation, then don't declare the class abstract. I thought the whole point of abstract classes was that they can't be instantiated. If it can be instantiated, then what does abstract even mean?Andrei Alexandrescu wrote:It may offer incomplete functionality that is to be reused and enhanced by descendants. AndreiConsider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder? AndreiWhat's the point of marking fun() abstract if it has an implementation, I thought the compiler disallowed that.
Oct 01 2009
Andrei Alexandrescu wrote:Jeremie Pelletier wrote:Then again, if it offers incomplete functionality why would you want it to be instantiable? Instantiable should mean you can use this guy no? I tried hard to think of a reason but can't find any.Andrei Alexandrescu wrote:It may offer incomplete functionality that is to be reused and enhanced by descendants. AndreiConsider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder? AndreiWhat's the point of marking fun() abstract if it has an implementation, I thought the compiler disallowed that.
Oct 01 2009
Lutger wrote:Andrei Alexandrescu wrote:Exactly, I only mark members as abstract if they have no implementations, if any members have an incomplete implementation then i mark the class as abstract. I never had any problems that way. Besides it just makes no sense to have an implemented member as abstract.Jeremie Pelletier wrote:Then again, if it offers incomplete functionality why would you want it to be instantiable? Instantiable should mean you can use this guy no? I tried hard to think of a reason but can't find any.Andrei Alexandrescu wrote:It may offer incomplete functionality that is to be reused and enhanced by descendants. AndreiConsider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder? AndreiWhat's the point of marking fun() abstract if it has an implementation, I thought the compiler disallowed that.
Oct 01 2009
On Thu, Oct 1, 2009 at 4:30 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:Consider: class A { =A0 =A0abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder?Uh... why?
Oct 01 2009
Jarrett Billingsley wrote:On Thu, Oct 1, 2009 at 4:30 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:Because I want to give a good argument one way or another in TDPL. FWIW, "I can't imagine why you'd ever..." or "Never needed that" are not strong enough arguments. AndreiConsider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder?Uh... why?
Oct 01 2009
On Thu, Oct 1, 2009 at 8:49 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:Jarrett Billingsley wrote:hOn Thu, Oct 1, 2009 at 4:30 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:Consider: class A { =A0 abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, althoug="IBecause I want to give a good argument one way or another in TDPL. FWIW, =technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder?Uh... why?can't imagine why you'd ever..." or "Never needed that" are not strong enough arguments.But.. you mark something abstract when you want it to be .. abstract. How would you argue that abstract is basically a no-op when used on methods with bodies? And there's a reasonable use for it, so why suddenly allow something that.. doesn't really make all that much sense to begin with?
Oct 01 2009
Jarrett Billingsley wrote:On Thu, Oct 1, 2009 at 8:49 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:It's not a no-op. Try it. AndreiJarrett Billingsley wrote:But.. you mark something abstract when you want it to be .. abstract. How would you argue that abstract is basically a no-op when used on methods with bodies?On Thu, Oct 1, 2009 at 4:30 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:Because I want to give a good argument one way or another in TDPL. FWIW, "I can't imagine why you'd ever..." or "Never needed that" are not strong enough arguments.Consider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder?Uh... why?
Oct 01 2009
On Thu, Oct 1, 2009 at 11:48 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:Yeah, not *currently*, but isn't that what you're proposing?But.. you mark something abstract when you want it to be .. abstract. How would you argue that abstract is basically a no-op when used on methods with bodies?It's not a no-op. Try it.
Oct 02 2009
Jarrett Billingsley wrote:On Thu, Oct 1, 2009 at 11:48 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:No. I think it would help going back to my original message instead of asking one-liner questions. This would work much better in real life, but it's a time sink in a newsgroup. You spend five seconds on asking a question with a foregone answer just because you don't want to invest fifteen seconds in re-reading my initial post, and then you have me spend five minutes explain things again. It's counter-productive. If a class defines an abstract method and also provides a body for it, it still requires the derived class to override the method. So abstract still has some meaning. On the other hand, technically such a class would become instantiable because it defines all of its methods. I wanted to explain that, however, that wouldn't be a good idea because... and here's where 1-2 good examples would have helped. I guess I'm going to drop it. AndreiYeah, not *currently*, but isn't that what you're proposing?But.. you mark something abstract when you want it to be .. abstract. How would you argue that abstract is basically a no-op when used on methods with bodies?It's not a no-op. Try it.
Oct 02 2009
On Fri, Oct 2, 2009 at 11:00 AM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:No. I think it would help going back to my original message instead of asking one-liner questions. This would work much better in real life, but it's a time sink in a newsgroup. You spend five seconds on asking a question with a foregone answer just because you don't want to invest fifteen seconds in re-reading my initial post, and then you have me spend five minutes explain things again. It's counter-productive. If a class defines an abstract method and also provides a body for it, it still requires the derived class to override the method. So abstract still has some meaning.Yes, I see now the parenthesized "requires overriding in derivees" now.On the other hand, technically such a class would become instantiable because it defines all of its methods. I wanted to explain that, however, that wouldn't be a good idea because... and here's where 1-2 good examples would have helped. I guess I'm going to drop it.Speaking of counterproductive timesinks, why would you bring up a proposal only to argue that it's a bad idea?
Oct 02 2009
Jarrett Billingsley wrote:On Fri, Oct 2, 2009 at 11:00 AM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:It was a question, you one-liner-asker you. AndreiNo. I think it would help going back to my original message instead of asking one-liner questions. This would work much better in real life, but it's a time sink in a newsgroup. You spend five seconds on asking a question with a foregone answer just because you don't want to invest fifteen seconds in re-reading my initial post, and then you have me spend five minutes explain things again. It's counter-productive. If a class defines an abstract method and also provides a body for it, it still requires the derived class to override the method. So abstract still has some meaning.Yes, I see now the parenthesized "requires overriding in derivees" now.On the other hand, technically such a class would become instantiable because it defines all of its methods. I wanted to explain that, however, that wouldn't be a good idea because... and here's where 1-2 good examples would have helped. I guess I'm going to drop it.Speaking of counterproductive timesinks, why would you bring up a proposal only to argue that it's a bad idea?
Oct 02 2009
On Fri, Oct 2, 2009 at 1:34 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:It was a question, you one-liner-asker you.What? ;)
Oct 02 2009
Andrei Alexandrescu wrote:Jarrett Billingsley wrote:Umm... so it defines a body that will never be used because that class can't be instantiated and the method must be redefined by subclasses? Isn't that the same as "doesn't provide a body"?On Thu, Oct 1, 2009 at 11:48 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:No. I think it would help going back to my original message instead of asking one-liner questions. This would work much better in real life, but it's a time sink in a newsgroup. You spend five seconds on asking a question with a foregone answer just because you don't want to invest fifteen seconds in re-reading my initial post, and then you have me spend five minutes explain things again. It's counter-productive. If a class defines an abstract method and also provides a body for it, it still requires the derived class to override the method. So abstract still has some meaning.Yeah, not *currently*, but isn't that what you're proposing?But.. you mark something abstract when you want it to be .. abstract. How would you argue that abstract is basically a no-op when used on methods with bodies?It's not a no-op. Try it.
Oct 03 2009
Ary Borenszweig wrote:Andrei Alexandrescu wrote:import std.stdio; class A { abstract void fun() { writeln("wyda"); } } class B : A { void fun() { A.fun(); } } unittest { A a = new B; a.fun(); a.A.fun(); } AndreiJarrett Billingsley wrote:Umm... so it defines a body that will never be used because that class can't be instantiated and the method must be redefined by subclasses? Isn't that the same as "doesn't provide a body"?On Thu, Oct 1, 2009 at 11:48 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:No. I think it would help going back to my original message instead of asking one-liner questions. This would work much better in real life, but it's a time sink in a newsgroup. You spend five seconds on asking a question with a foregone answer just because you don't want to invest fifteen seconds in re-reading my initial post, and then you have me spend five minutes explain things again. It's counter-productive. If a class defines an abstract method and also provides a body for it, it still requires the derived class to override the method. So abstract still has some meaning.Yeah, not *currently*, but isn't that what you're proposing?But.. you mark something abstract when you want it to be .. abstract. How would you argue that abstract is basically a no-op when used on methods with bodies?It's not a no-op. Try it.
Oct 03 2009
"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message news:ha8beq$2tn9$1 digitalmars.com...Ary Borenszweig wrote:Not a rhetorical or a loaded question: Has that sort of thing ever been useful?Umm... so it defines a body that will never be used because that class can't be instantiated and the method must be redefined by subclasses? Isn't that the same as "doesn't provide a body"?import std.stdio; class A { abstract void fun() { writeln("wyda"); } } class B : A { void fun() { A.fun(); } } unittest { A a = new B; a.fun(); a.A.fun(); }
Oct 04 2009
Nick Sabalausky wrote:"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message news:ha8beq$2tn9$1 digitalmars.com...I was wondering the same. It's also very bug prone because when overriding the function you must remember to invoke the super method, a thing you can forget.Ary Borenszweig wrote:Not a rhetorical or a loaded question: Has that sort of thing ever been useful?Umm... so it defines a body that will never be used because that class can't be instantiated and the method must be redefined by subclasses? Isn't that the same as "doesn't provide a body"?import std.stdio; class A { abstract void fun() { writeln("wyda"); } } class B : A { void fun() { A.fun(); } } unittest { A a = new B; a.fun(); a.A.fun(); }
Oct 05 2009
On 2-10-2009 4:30, Andrei Alexandrescu wrote:Consider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder? AndreiIf it were instantiable, what would be the difference between "abstract" and "virtual"? L.
Oct 01 2009
On 2-10-2009 8:32, Lionello Lunesu wrote:On 2-10-2009 4:30, Andrei Alexandrescu wrote:OK, so I reread your post and yes, "it requires overriding in derivees." Derived classes would still be allowed to do super.fun(), calling the 'abstract' class's implementation. It's like NVI, only worse? L.Consider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder? AndreiIf it were instantiable, what would be the difference between "abstract" and "virtual"?
Oct 02 2009
On Thu, 01 Oct 2009 16:30:43 -0400, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:Consider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder?If you want to force a class to be abstract, even though it technically could be concrete given it has implementations for all functions, you could allow that by marking the class abstract. i.e.: abstract class A { void fun() {} } The other side, allowing A to be instantiated, makes no sense whatsoever. If you (as a class designer) want something to be instantiated, and it has all methods implemented, why mark it abstract? IMO, it should be a compiler error to give implementation to an abstract method. -Steve
Oct 02 2009
Andrei Alexandrescu Wrote:Jarrett Billingsley wrote:On Thu, Oct 1, 2009 at 4:30 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:Consider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder?Uh... why?Because I want to give a good argument one way or another in TDPL. FWIW, "I can't imagine why you'd ever..." or "Never needed that" are not strong enough arguments.AndreiI had a brilliant maths teacher at high school. Every now and then someone would ask a question which everyone else though was silly. Often he would jokingly reply, "Good question. Next question." and then, after a pause, explore the subtleties of the "silly" question to everyone's total amazement. There was one really, really smart guy who asked enough "silly" questions and ended up getting his Ph.D. in Mathematics before the age of 20."Because I want to give a good argument one way or another in TDPL."Fair enough, Andrei. I'm trying hard with the following use case to demonstrate that instantiation of the abstract is possibly useful. Hope this helps or leads to further insight. class NonNegativeInteger { static NonNegativeInteger infinity; static this() { infinity = new NonNegativeInteger(); } /+abstract+/ long value() { // In Andrei's scenario, this method is marked abstract // so we don't really care what we return here; // For moment comment out abstract keyword so that the // initialization of the singleton instance of // NonNegativeInteger, infinity, gets past the current // compiler. // Other than the private singleton instantiation inside // the static this block, no external code is allowed to // call new NonNegativeInteger() because the class // is, for all intensive purposes, abstract (once the // abstract keyword is uncommented). assert(false); return infinity.value; } void print() { writefln( "Infinity"); } } class FiniteNonNegativeInteger: NonNegativeInteger { private long val; this( long value) { val = value; } long value() { return val; } NonNegativeInteger opDiv( NonNegativeInteger divisor) { return (divisor.value != 0) ? new FiniteNonNegativeInteger( value / divisor.value) : NonNegativeInteger.infinity; } void print() { writefln( "%d", val); } } void main() { auto hundred = new FiniteNonNegativeInteger( 100); auto two = new FiniteNonNegativeInteger( 2); auto zero = new FiniteNonNegativeInteger( 0); auto fifty = hundred / two; auto infinity = hundred / zero; hundred.print(); two.print(); fifty.print(); infinity.print(); } Outputs: 100 2 50 Infinity Cheers Justin Johansson.
Oct 02 2009
Justin Johansson Wrote:Andrei Alexandrescu Wrote:Just in case I'm misunderstood .. I don't believe the concept is particularly useful; just that Andrei was looking for some example, presumably to expand upon in TDPL, and my sample was just an exploratory idea. Obviously the sample that I dreamed up results in an infinite loop if the "abstract" base class value() method is actually called. -- JustinJarrett Billingsley wrote:On Thu, Oct 1, 2009 at 4:30 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:Consider: class A { abstract void fun() {} } The class defines a function that is at the same time abstract (so it requires overriding in derivees) and has implementation. Currently the compiler disallows creation of objects of type A, although technically that is feasible given that A defines the abstract method. Should A be instantiable? What designs would that help or hinder?Uh... why?Because I want to give a good argument one way or another in TDPL. FWIW, "I can't imagine why you'd ever..." or "Never needed that" are not strong enough arguments.AndreiFair enough, Andrei. I'm trying hard with the following use case to demonstrate that instantiation of the abstract is possibly useful. Hope this helps or leads to further insight.
Oct 03 2009