www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - auto declarations

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
auto a = 1, b = null;

int a = 1, *b = null;


The first is accepted by dmd, and it should result in typeof(a) == int 
and typeof(b) == void*. It is somewhat contradictory to the error 
message resulting from the second:

multiple declarations must have the same type, not int and int*

I am skeptical of dmd's permitting the first. Does anyone else see any 
utility in it?
Jan 07 2011
next sibling parent Piotr Szturmaj <bncrbme jadamspam.pl> writes:
Ellery Newcomer wrote:
 auto a = 1, b = null;

 int a = 1, *b = null;


 The first is accepted by dmd, and it should result in typeof(a) == int
 and typeof(b) == void*. It is somewhat contradictory to the error
 message resulting from the second:

 multiple declarations must have the same type, not int and int*

 I am skeptical of dmd's permitting the first. Does anyone else see any
 utility in it?

Personally, I like it. In second line you specify int type, and list of *int* variables. In first line you specify list of initialized variables which types should be inferred automatically. I see no reason why first line should not be permitted. If one would need several variables of one type, why he wouldn't specify exact type instead of using 'auto'?
Jan 07 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, January 07, 2011 13:32:42 Ellery Newcomer wrote:
 auto a = 1, b = null;
 
 int a = 1, *b = null;
 
 
 The first is accepted by dmd, and it should result in typeof(a) == int
 and typeof(b) == void*. It is somewhat contradictory to the error
 message resulting from the second:
 
 multiple declarations must have the same type, not int and int*
 
 I am skeptical of dmd's permitting the first. Does anyone else see any
 utility in it? 

The second should definitely _not_ be allowed. * definitely goes with the type in D (as it should have in C), not the variable. So, the *b = null makes no sense. However, I'm vere suprised that the first one succeeds. I think that it should be reported as a bug. All variables declared on the same line are supposed to have the same type. - Jonathan M Davis
Jan 07 2011
prev sibling next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Ellery Newcomer:

 int a = 1, *b = null;

Walter has disallowed code like this in D because in C it is a well known source of bugs (so much that C style guides strongly suggest to declare only each variable in a distinct statement and line of code).
 auto a = 1, b = null;

I have discussed this with other people some time ago. At first I don't like this, it's against the D rule of not allowing different types to be initialized in the same statement. It may cause some bugs. Example: if you write a line of code like, meaning it to initialize six double variables both you have a bug: auto x1=1., x2=2., x3=3., x4=4., x5=5, x6=6.; But you are playing with fire. Better to be safer and write: double x1=1., x2=2., x3=3., x4=4., x5=5, x6=6.; Or even: double x1, x2, x3, x4, x5, x6; x1 = 1.0; x2 = 2.0; x3 = 3.0; x4 = 4.0; x5 = 5.0; x6 = 6.0; Or: double[6] x; foreach (i, ref xi; x) xi = i + 1; Or: double[6] x = array(iota(1.0, 7.0));
 The first is accepted by dmd, and it should result in typeof(a) == int 
 and typeof(b) == void*. It is somewhat contradictory to the error 
 message resulting from the second:
 
 multiple declarations must have the same type, not int and int*

Error messages in DMD are a draft :-) I have opened many bug reports that suggest to improve them. Here the error message is not fully correct, but I think it's acceptable. To improve it the D compiler may also give a number of the error, and a manual of the errors may explain that indeed the "auto" is allowed to instantiate different types, etc. I don't know why DMD errors are not numbered ad in C#. Bye, bearophile
Jan 08 2011
prev sibling next sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
On Fri, 07 Jan 2011 16:30:24 -0800, Jonathan M Davis wrote:

 On Friday, January 07, 2011 13:32:42 Ellery Newcomer wrote:
 auto a = 1, b = null;
 
 int a = 1, *b = null;
 
 
 [...]

[...] However, I'm vere suprised that the first one succeeds. I think that it should be reported as a bug. All variables declared on the same line are supposed to have the same type.

If I remember correctly, TDPL explicitly states that you may use automatic type inference to declare variables of different types in one line. -Lars
Jan 10 2011
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday 10 January 2011 01:59:34 Lars T. Kyllingstad wrote:
 On Fri, 07 Jan 2011 16:30:24 -0800, Jonathan M Davis wrote:
 On Friday, January 07, 2011 13:32:42 Ellery Newcomer wrote:
 auto a = 1, b = null;
 
 int a = 1, *b = null;
 
 
 [...]

[...] However, I'm vere suprised that the first one succeeds. I think that it should be reported as a bug. All variables declared on the same line are supposed to have the same type.

If I remember correctly, TDPL explicitly states that you may use automatic type inference to declare variables of different types in one line.

Well, it may. I don't know. I don't really believe in declaring multiple variables on one line in normal circumstances anyway. However, it's definitely an inconsistency in the language, and I would be inclined to argue that it should be removed and TDPL errata-ed if it does mention this "feature." I don't see any real benefit of allowing this. - Jonathan M Davis
Jan 10 2011