Suggestion: shortcut for 'new X'
Kristian
kjkilpi at gmail.com
Thu Aug 24 13:41:34 PDT 2006
On Thu, 24 Aug 2006 23:30:25 +0300, Ivan Senji
<ivan.senji_REMOVE_ at _THIS__gmail.com> wrote:
> Kristian wrote:
>> On Thu, 24 Aug 2006 21:17:23 +0300, BCS <BCS at 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'.
>
> Well true, but
> auto obj = new Obj();
>
> is not much longer, also isn't redundant and it also tells everyone what
> type 'obj' is.
That's right, but it raises a matter of (in)consistence in some cases as I
wrote in my earlier post:
>
> 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'.)
More information about the Digitalmars-d
mailing list