www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - auto

reply Hoenir <mrmocool gmx.de> writes:
I still can't get the purpose of "auto".
I know it makes it possible to e.g. write "auto y = 4u;" but why not 
just "uint y = 4;"?
Jun 20 2007
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 20 Jun 2007 14:20:43 +0200, Hoenir wrote:

 I still can't get the purpose of "auto".
 I know it makes it possible to e.g. write "auto y = 4u;" but why not 
 just "uint y = 4;"?
auto y = someFunc(); // What type is it now? -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
Jun 20 2007
parent reply Ary Manzana <ary esperanto.org.ar> writes:
Derek Parnell escribió:
 On Wed, 20 Jun 2007 14:20:43 +0200, Hoenir wrote:
 
 I still can't get the purpose of "auto".
 I know it makes it possible to e.g. write "auto y = 4u;" but why not 
 just "uint y = 4;"?
auto y = someFunc(); // What type is it now?
// What can you do with y now, if you don't know the type?
Jun 20 2007
next sibling parent BCS <BCS pathlink.com> writes:
Ary Manzana wrote:
 Derek Parnell escribió:

 On Wed, 20 Jun 2007 14:20:43 +0200, Hoenir wrote:

 I still can't get the purpose of "auto".
 I know it makes it possible to e.g. write "auto y = 4u;" but why not
 just "uint y = 4;"?
auto y = someFunc(); // What type is it now?
// What can you do with y now, if you don't know the type?
whatever you want auto z = y * 2; auto x = z*y; return x + y + z; With a little care and a working knowledge of the typing rules of expressions you can get a lot done without ever using a actual type. This is of a lot of use if you are doing template code where you really can't known the type until compile time.
Jun 20 2007
prev sibling parent reply Derek Parnell <derek nomail.afraid.org> writes:
On Wed, 20 Jun 2007 21:37:44 -0200, Ary Manzana wrote:

 Derek Parnell escribió:
 On Wed, 20 Jun 2007 14:20:43 +0200, Hoenir wrote:
 
 I still can't get the purpose of "auto".
 I know it makes it possible to e.g. write "auto y = 4u;" but why not 
 just "uint y = 4;"?
auto y = someFunc(); // What type is it now?
// What can you do with y now, if you don't know the type?
Pass it to a function that does know its type. void funcA(int X) { } void funcA(float X) { } void funcA(char[] X) { } auto y = someFunc(); funcA(y); -- Derek (skype: derek.j.parnell) Melbourne, Australia 21/06/2007 12:30:47 PM
Jun 20 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Derek Parnell wrote:
 On Wed, 20 Jun 2007 21:37:44 -0200, Ary Manzana wrote:
 
 Derek Parnell escribió:
 On Wed, 20 Jun 2007 14:20:43 +0200, Hoenir wrote:

 I still can't get the purpose of "auto".
 I know it makes it possible to e.g. write "auto y = 4u;" but why not 
 just "uint y = 4;"?
auto y = someFunc(); // What type is it now?
// What can you do with y now, if you don't know the type?
Pass it to a function that does know its type.
I *think* the point was just that the code becomes less readable without any obvious indication of what type you've got. Sure you can do all the same things you could do it the type were explicit, but someone else asked to fix this code is going to have to track down "someFunc()" to see what it returns. Lacking any particular extenuating circumstances, I agree that that making types explicit is better than using auto. But auto makes sense when 1) the actual type is a built-in (auto x = foo.length) 2) the actual type is obvious (auto x = new MyClass()) 3) the actual type is huge/untypeable like ( Node!(Node!(Node!(Expr!(Terminal!(1),Terminal!(2))))) x = expr!(); 4) the actual type is unknowable (some template usages) or 5) when you're in a real hurry and don't have time to figure out what the actual type is. :-) --bb
Jun 21 2007
next sibling parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Bill Baxter wrote:
 Lacking any particular extenuating circumstances, I agree that that 
 making types explicit is better than using auto.  But auto makes sense when
[list] How about: 6) When the actual type may change later, and you want to make sure the variable is of the right type? I've often found this to be very useful, especially since some implicit conversions can change the value (e.g. long -> int, signed/unsigned).
Jun 22 2007
prev sibling parent Ary Manzana <ary esperanto.org.ar> writes:
Bill Baxter escribió:
 Derek Parnell wrote:
 On Wed, 20 Jun 2007 21:37:44 -0200, Ary Manzana wrote:

 Derek Parnell escribió:
 On Wed, 20 Jun 2007 14:20:43 +0200, Hoenir wrote:

 I still can't get the purpose of "auto".
 I know it makes it possible to e.g. write "auto y = 4u;" but why 
 not just "uint y = 4;"?
auto y = someFunc(); // What type is it now?
// What can you do with y now, if you don't know the type?
Pass it to a function that does know its type.
I *think* the point was just that the code becomes less readable without any obvious indication of what type you've got. Sure you can do all the same things you could do it the type were explicit, but someone else asked to fix this code is going to have to track down "someFunc()" to see what it returns.
Thanks for this! This is exacly my thought, although I couldn't have expressed myself that well. I also agree with the situations you describe below, and also with number 6 of Frits. I'll not use it anywhere else, since having the type is more self documenting in a function.
 
 Lacking any particular extenuating circumstances, I agree that that 
 making types explicit is better than using auto.  But auto makes sense when
 1) the actual type is a built-in (auto x = foo.length)
 2) the actual type is obvious (auto x = new MyClass())
 3) the actual type is huge/untypeable like
    ( Node!(Node!(Node!(Expr!(Terminal!(1),Terminal!(2))))) x = expr!();
 4) the actual type is unknowable (some template usages)
 
 or
 
 5) when you're in a real hurry and don't have time to figure out what 
 the actual type is.  :-)
 
 --bb
Jun 22 2007
prev sibling next sibling parent mwarning <moritzwarning spam.web.de> writes:
Hoenir Wrote:

 I still can't get the purpose of "auto".
 I know it makes it possible to e.g. write "auto y = 4u;" but why not 
 just "uint y = 4;"?
"auto" is very usefull for cases like: auto x = new MyNewClass!(Foo, Bar)(); or auto y = getSomething(); doSmomething(y);
Jun 20 2007
prev sibling next sibling parent Jari-Matti =?ISO-8859-1?Q?M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
Hoenir wrote:

 I still can't get the purpose of "auto".
 I know it makes it possible to e.g. write "auto y = 4u;" but why not
 just "uint y = 4;"?
One day you may want to write class foo(a = int, b = int) { } void main() { auto bar = new foo!(foo!( foo!(foo!(), foo!()), foo!(foo!(), foo!()) ), foo!(foo!(), foo!())); }
Jun 20 2007
prev sibling next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Hoenir" <mrmocool gmx.de> wrote in message 
news:f5b64b$1aeq$1 digitalmars.com...
I still can't get the purpose of "auto".
 I know it makes it possible to e.g. write "auto y = 4u;" but why not just 
 "uint y = 4;"?
I agree in this case, and I think some people get a little type-inference-happy. But it's very useful in the cases that Derek and mwarning have posted, and I use it a lot in templates as well (rather than picking apart a complex type to determine the correct type for a variable, just use auto).
Jun 20 2007
parent Hoenir <mrmocool gmx.de> writes:
Jarrett Billingsley wrote:
 I agree in this case, and I think some people get a little 
 type-inference-happy.  But it's very useful in the cases that Derek and 
 mwarning have posted, and I use it a lot in templates as well (rather than 
 picking apart a complex type to determine the correct type for a variable, 
 just use auto). 
 
I see. This makes sense. Thank you all. :)
Jun 20 2007
prev sibling parent reply janderson <askme me.com> writes:
Hoenir wrote:
 I still can't get the purpose of "auto".
 I know it makes it possible to e.g. write "auto y = 4u;" but why not 
 just "uint y = 4;"?
Its a generic/maintainable way of programming. It means you can change the types your using without having to change ever reference to it. It means you can copy paste or template a piece of code and have it work with little change. It gets even more useful in larger programs when you start to layer this sort of general programming. I hope that helps.
Jun 21 2007
parent janderson <askme me.com> writes:
janderson wrote:
 Hoenir wrote:
 I still can't get the purpose of "auto".
 I know it makes it possible to e.g. write "auto y = 4u;" but why not 
 just "uint y = 4;"?
Its a generic/maintainable way of programming. It means you can change the types your using without having to change ever reference to it. It means you can copy paste or template a piece of code and have it work with little change. It gets even more useful in larger programs when you start to layer this sort of general programming. I hope that helps.
I should add, in the same vain (as others have mentioned in other ways), its useful for creating code that will work even when someone changes the interface. -Joel
Jun 21 2007