Jump to page: 1 2
Thread overview
defered new feature
Oct 23, 2008
BCS
Oct 23, 2008
BCS
Oct 23, 2008
BCS
Oct 24, 2008
downs
Oct 24, 2008
BCS
Oct 24, 2008
downs
Oct 24, 2008
BCS
October 23, 2008
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){}
> }


October 23, 2008
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
October 23, 2008
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.


October 23, 2008
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

October 23, 2008
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
> 


October 23, 2008
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

October 24, 2008
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 */ } }
October 24, 2008
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 /* */)


October 24, 2008
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
October 24, 2008
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 ~ $
« First   ‹ Prev
1 2