www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - defered new feature

reply BCS <ao pathlink.com> writes:
A number of times I have found my self wanting to have "new C(args)" return 
a class derived from C. I know this can be done with a static function or 
the like but syntactically, it's unappealing.

My suggestion is that, with some kind of flag, constructors be allowed and 
required to construct an object and return it.

One use cases:

 class C
 {
    abstract void F(); /// abstract class
    abstract this(int i) /// "abstract" this is just one idea, others would 
work as well
    {
        if(i < 0)
           return new N(-i);
        else
           return new P(i);
    }
 }

 private class N : C
 {
    void F();
    this(uint i){}
 }

 private class P : C
 {
    void F();
    this(uint i){}
 }
Oct 23 2008
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
BCS wrote:
 A number of times I have found my self wanting to have "new C(args)" 
 return a class derived from C. I know this can be done with a static 
 function or the like but syntactically, it's unappealing.
I'd say the stupid "new" is unappealing. Andrei
Oct 23 2008
parent reply BCS <ao pathlink.com> writes:
Reply to Andrei,

 BCS wrote:
 
 A number of times I have found my self wanting to have "new C(args)"
 return a class derived from C. I know this can be done with a static
 function or the like but syntactically, it's unappealing.
 
I'd say the stupid "new" is unappealing. Andrei
So you don't like "new" all together? Ok, then morph the idea to allow "the constructor call syntax" to do the above.
Oct 23 2008
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
BCS wrote:
 Reply to Andrei,
 
 BCS wrote:

 A number of times I have found my self wanting to have "new C(args)"
 return a class derived from C. I know this can be done with a static
 function or the like but syntactically, it's unappealing.
I'd say the stupid "new" is unappealing. Andrei
So you don't like "new" all together?
I consider it an unnecessary appendage and a waste of two keywords (considering delete too).
 Ok, then morph the idea to allow "the constructor call syntax" to do the 
 above.
Then we're pretty much down to functions :o). Andrei
Oct 23 2008
parent reply BCS <ao pathlink.com> writes:
Reply to Andrei,

 BCS wrote:
 
 Reply to Andrei,
 
 BCS wrote:
 
 A number of times I have found my self wanting to have "new
 C(args)" return a class derived from C. I know this can be done
 with a static function or the like but syntactically, it's
 unappealing.
 
I'd say the stupid "new" is unappealing. Andrei
So you don't like "new" all together?
I consider it an unnecessary appendage and a waste of two keywords (considering delete too).
You can't dump delete (unless you propose having a delete property) because D still allows manual memory management.
 Ok, then morph the idea to allow "the constructor call syntax" to do
 the above.
 
Then we're pretty much down to functions :o).
Yes they might look the same, but /some/ magic will be needed to make overloading work because constructors already have some magic in them.
 Andrei
 
Oct 23 2008
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
BCS wrote:
 Reply to Andrei,
 
 BCS wrote:

 Reply to Andrei,

 BCS wrote:

 A number of times I have found my self wanting to have "new
 C(args)" return a class derived from C. I know this can be done
 with a static function or the like but syntactically, it's
 unappealing.
I'd say the stupid "new" is unappealing. Andrei
So you don't like "new" all together?
I consider it an unnecessary appendage and a waste of two keywords (considering delete too).
You can't dump delete (unless you propose having a delete property) because D still allows manual memory management.
It's a function!!! Andrei
Oct 23 2008
prev sibling parent reply downs <default_357-line yahoo.de> writes:
BCS wrote:
 A number of times I have found my self wanting to have "new C(args)"
 return a class derived from C. I know this can be done with a static
 function or the like but syntactically, it's unappealing.
Amusing fact: you can assign to "this" in the constructor. typedef bool Bogus; const Bogus bogus = false; class A { this() { this = new B; } this(Bogus) { } } class B : A { this() { super(bogus); /* rest of constructor */ } }
Oct 23 2008
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Downs,

 class A { this() { this = new B; } this(Bogus) { } } 
 class B : A { this() { super(bogus); /* rest of constructor */ } }
 
to bad it doesn't compile :( http://codepad.org/xkBAzQ2c -> SEG-V (if you drop the /* */)
Oct 23 2008
parent reply downs <default_357-line yahoo.de> writes:
BCS wrote:
 Reply to Downs,
 
 class A { this() { this = new B; } this(Bogus) { } } class B : A {
 this() { super(bogus); /* rest of constructor */ } }
to bad it doesn't compile :( http://codepad.org/xkBAzQ2c -> SEG-V (if you drop the /* */)
gentoo-pc ~ $ cat test113.d && echo "----" && gdc test113.d -o test113 && ./test113 module test113; typedef bool Bogus; const Bogus bogus = false; class A { this() { this = new B; } this(Bogus) { } } class B : A { this() { super(bogus); /* rest of constructor */ } } import std.stdio; void main() { writefln(new A); } ---- test113.B gentoo-pc ~ $
Oct 24 2008
parent BCS <ao pathlink.com> writes:
Reply to Downs,

 BCS wrote:
 
 to bad it doesn't compile :(
Oops. http://codepad.org/CYTJzhJc
 gentoo-pc ~ $ cat test113.d && echo "----" && gdc test113.d -o test113
 && ./test113 module test113;
 
 typedef bool Bogus;
 const Bogus bogus = false;
 class A { this() { this = new B; } this(Bogus) { } } class B : A {
 this() { super(bogus); /* rest of constructor */ } }
 
 import std.stdio;
 void main() { writefln(new A); }
 ----
 test113.B
 gentoo-pc ~ $
Oct 24 2008
prev sibling parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Thu, Oct 23, 2008 at 9:31 PM, downs <default_357-line yahoo.de> wrote:
 BCS wrote:
 A number of times I have found my self wanting to have "new C(args)"
 return a class derived from C. I know this can be done with a static
 function or the like but syntactically, it's unappealing.
Amusing fact: you can assign to "this" in the constructor. typedef bool Bogus; const Bogus bogus = false; class A { this() { this = new B; } this(Bogus) { } } class B : A { this() { super(bogus); /* rest of constructor */ } }
I once used this looong ago when I first came to D so that attempting to do a "new Texture(`some/file`)" twice on the same file would return the same texture object. Of course, at the time, it didn't occur to me that (1) that was horribly hackish or (2) that a static method would have been much better there XD
Oct 23 2008