Jump to page: 1 2
Thread overview
Suggestion: shortcut for 'new X'
Aug 24, 2006
Kristian
Aug 24, 2006
Oskar Linde
Aug 24, 2006
Kristian
Aug 24, 2006
BCS
Aug 24, 2006
Kristian
Aug 24, 2006
BCS
Aug 24, 2006
Kristian
Aug 24, 2006
Ivan Senji
Aug 24, 2006
Kristian
Aug 25, 2006
Ivan Senji
Aug 25, 2006
Kristian
Aug 24, 2006
freeagle
Aug 24, 2006
Kristian
Aug 24, 2006
Sean Kelly
Aug 25, 2006
Kristian
August 24, 2006
How about this:

    Obj obj1 = new Obj;
    Obj obj2 = new Obj(10);

could be reduced to:

    Obj obj1 = new;
    Obj obj2 = new(10);

This way only one class name would be required per variable. (That is, one at maximum: "Obj obj1 = new, obj2 = new(10);" is also valid of course.) Redundance would be reduced.

I know, it looked quite 'odd' for me too at first, but now it looks very natural, and even clearer than the longer way.


I'm not sure if using of this syntax should be possible outside the variable declarations. Later in a code "obj1 = new;" is less informative than "obj1 = new Obj;". However, someone may like it too... it's up to a programmer how to code (which is not always a good thing(tm) though).
August 24, 2006
Kristian wrote:
> How about this:
> 
>     Obj obj1 = new Obj;
>     Obj obj2 = new Obj(10);
> 
> could be reduced to:
> 
>     Obj obj1 = new;
>     Obj obj2 = new(10);
> 
> This way only one class name would be required per variable. (That is, one at maximum: "Obj obj1 = new, obj2 = new(10);" is also valid of course.) Redundance would be reduced.

You can already do:

auto obj1 = new Obj;
auto obj2 = new Obj(10);

which takes care of the redundant Obj.
August 24, 2006
On Thu, 24 Aug 2006 18:10:12 +0300, Oskar Linde <oskar.lindeREM@OVEgmail.com> wrote:

> Kristian wrote:
>> How about this:
>>      Obj obj1 = new Obj;
>>     Obj obj2 = new Obj(10);
>>  could be reduced to:
>>      Obj obj1 = new;
>>     Obj obj2 = new(10);
>>  This way only one class name would be required per variable. (That is, one at maximum: "Obj obj1 = new, obj2 = new(10);" is also valid of course.) Redundance would be reduced.
>
> You can already do:
>
> auto obj1 = new Obj;
> auto obj2 = new Obj(10);
>
> which takes care of the redundant Obj.

Yes, and it's nice.
But you cannot always use auto.

Obj func() {
    Obj ret = new;

    ret.doSomething();
    ...
    return(ret);
}

That would be nice too. :)
August 24, 2006
Kristian wrote:
> On Thu, 24 Aug 2006 18:10:12 +0300, Oskar Linde  <oskar.lindeREM@OVEgmail.com> wrote:
> 
>> Kristian wrote:
>>
>>> How about this:
>>>      Obj obj1 = new Obj;
>>>     Obj obj2 = new Obj(10);
>>>  could be reduced to:
>>>      Obj obj1 = new;
>>>     Obj obj2 = new(10);
>>>  This way only one class name would be required per variable. (That is,  one at maximum: "Obj obj1 = new, obj2 = new(10);" is also valid of  course.) Redundance would be reduced.
>>
>>
>> You can already do:
>>
>> auto obj1 = new Obj;
>> auto obj2 = new Obj(10);
>>
>> which takes care of the redundant Obj.
> 
> 
> Yes, and it's nice.
> But you cannot always use auto.
> 
> Obj func() {
>     Obj ret = new;
> 
>     ret.doSomething();
>     ...
>     return(ret);
> }
> 
> That would be nice too. :)

IIRC this works:

Obj func() {
    auto ret = new Obj;

    ret.doSomething();
    ...
    return(ret);
}
August 24, 2006
Kristian wrote:
> On Thu, 24 Aug 2006 18:10:12 +0300, Oskar Linde <oskar.lindeREM@OVEgmail.com> wrote:
> 
>> Kristian wrote:
>>> How about this:
>>>      Obj obj1 = new Obj;
>>>     Obj obj2 = new Obj(10);
>>>  could be reduced to:
>>>      Obj obj1 = new;
>>>     Obj obj2 = new(10);
>>>  This way only one class name would be required per variable. (That is, one at maximum: "Obj obj1 = new, obj2 = new(10);" is also valid of course.) Redundance would be reduced.
>>
>> You can already do:
>>
>> auto obj1 = new Obj;
>> auto obj2 = new Obj(10);
>>
>> which takes care of the redundant Obj.
> 
> Yes, and it's nice.
> But you cannot always use auto.
> 
> Obj func() {
>     Obj ret = new;
> 
>     ret.doSomething();
>     ...
>     return(ret);
> }
> 
> That would be nice too. :)

And if you want to create object of inherited class and store it into referenco to base class??

class A
{
}

class B : A
{
}

A a = new/* B */; ??
August 24, 2006
On Thu, 24 Aug 2006 19:08:22 +0300, BCS <BCS@pathlink.com> wrote:

> Kristian wrote:
>> On Thu, 24 Aug 2006 18:10:12 +0300, Oskar Linde  <oskar.lindeREM@OVEgmail.com> wrote:
>>
>>> Kristian wrote:
>>>
>>>> How about this:
>>>>      Obj obj1 = new Obj;
>>>>     Obj obj2 = new Obj(10);
>>>>  could be reduced to:
>>>>      Obj obj1 = new;
>>>>     Obj obj2 = new(10);
>>>>  This way only one class name would be required per variable. (That is,  one at maximum: "Obj obj1 = new, obj2 = new(10);" is also valid of  course.) Redundance would be reduced.
>>>
>>>
>>> You can already do:
>>>
>>> auto obj1 = new Obj;
>>> auto obj2 = new Obj(10);
>>>
>>> which takes care of the redundant Obj.
>>   Yes, and it's nice.
>> But you cannot always use auto.
>>  Obj func() {
>>     Obj ret = new;
>>      ret.doSomething();
>>     ...
>>     return(ret);
>> }
>>  That would be nice too. :)
>
> IIRC this works:
>
> Obj func() {
>      auto ret = new Obj;
>
>      ret.doSomething();
>      ...
>      return(ret);
> }

It seems that you're right. Thanks for pointing it out. :)

However, I don't think that it's not good programming style to use auto a lot. :/

Consider the following:

Obj func2() {...}

void func() {
    auto obj = func2();
}

When looking at 'func()' only it's impossible to know the type of 'obj'.

Of course I could write "auto Obj obj" or "Obj obj", but it's inconsistent with 'anonymous autos':

void func() {
    auto Obj obj1 = func();
    auto obj2 = new Obj;
}

I would really prefer the following:

void func() {
    Obj obj1 = func();
    Obj obj2 = new;
    Obj obj3;
}

Or:

void func() {
    auto Obj obj1 = func();
    auto Obj obj2 = new;
    auto Obj obj3;
}

(Note the usage of the shortcut of 'new'.)
August 24, 2006
On Thu, 24 Aug 2006 19:06:29 +0300, freeagle <freeagle@inmail.sk> wrote:

> Kristian wrote:
>> On Thu, 24 Aug 2006 18:10:12 +0300, Oskar Linde <oskar.lindeREM@OVEgmail.com> wrote:
>>
>>> Kristian wrote:
>>>> How about this:
>>>>      Obj obj1 = new Obj;
>>>>     Obj obj2 = new Obj(10);
>>>>  could be reduced to:
>>>>      Obj obj1 = new;
>>>>     Obj obj2 = new(10);
>>>>  This way only one class name would be required per variable. (That is, one at maximum: "Obj obj1 = new, obj2 = new(10);" is also valid of course.) Redundance would be reduced.
>>>
>>> You can already do:
>>>
>>> auto obj1 = new Obj;
>>> auto obj2 = new Obj(10);
>>>
>>> which takes care of the redundant Obj.
>>  Yes, and it's nice.
>> But you cannot always use auto.
>>  Obj func() {
>>     Obj ret = new;
>>      ret.doSomething();
>>     ...
>>     return(ret);
>> }
>>  That would be nice too. :)
>
> And if you want to create object of inherited class and store it into referenco to base class??
>
> class A
> {
> }
>
> class B : A
> {
> }
>
> A a = new/* B */; ??

If you write "A a = new;" it would equal to "A a = new A;"

If you would like to create an object of type B, then you have to use the longer (normal) version:

A a = new B;

There is no redundancy in "A a = new;" and "A a = new B;". However, there is redundancy in "A a = new A;".
August 24, 2006
Kristian wrote:
> 
> However, I don't think that it's good programming style to use auto a  lot. :/
> 

substitute excessive for a lot and I agree.

> Consider the following:
> 
> Obj func2() {...}
> 
> void func() {
>     auto obj = func2();
> }
> 
> When looking at 'func()' only it's impossible to know the type of 'obj'.

that can be a bit of a gotcha. I haven't done it but putting in a

static assert(is(obj : Obj));

could serve as a bit of type documentation and as a guard against API changes.
August 24, 2006
On Thu, 24 Aug 2006 21:17:23 +0300, BCS <BCS@pathlink.com> wrote:

> Kristian wrote:
>>  However, I don't think that it's good programming style to use auto a  lot. :/
>>
>
> substitute excessive for a lot and I agree.
>
>> Consider the following:
>>  Obj func2() {...}
>>  void func() {
>>     auto obj = func2();
>> }
>>  When looking at 'func()' only it's impossible to know the type of 'obj'.
>
> that can be a bit of a gotcha. I haven't done it but putting in a
>
> static assert(is(obj : Obj));
>
> could serve as a bit of type documentation and as a guard against API changes.

That's too hackish to my taste, and who have energy to write such long statements for simple variable declarations? ;)

Better have a simple, clear way to declare a variable, haven't it?

"Obj obj = new;" is short, non-redundant, and it tells everybody that 'obj' is of type 'Obj'.
August 24, 2006
Kristian wrote:
> How about this:
> 
>     Obj obj1 = new Obj;
>     Obj obj2 = new Obj(10);
> 
> could be reduced to:
> 
>     Obj obj1 = new;
>     Obj obj2 = new(10);
> 
> This way only one class name would be required per variable. (That is, one  at maximum: "Obj obj1 = new, obj2 = new(10);" is also valid of course.)  Redundance would be reduced.
> 
> I know, it looked quite 'odd' for me too at first, but now it looks very  natural, and even clearer than the longer way.
> 
> 
> I'm not sure if using of this syntax should be possible outside the  variable declarations. Later in a code "obj1 = new;" is less informative  than "obj1 = new Obj;". However, someone may like it too... it's up to a  programmer how to code (which is not always a good thing(tm) though).

Would this pose any ambiguities with custom class allocators?

-- Chris Nicholson-Sauls
« First   ‹ Prev
1 2