digitalmars.D - D's auto keyword
- Justin Johansson (14/14) Jan 13 2010 Happy New Year 2010 Everybody.
- Justin Johansson (3/3) Jan 13 2010 And sorry for saying btw twice in the same post.
- Simen kjaeraas (4/7) Jan 13 2010 You are not forgiven. Post moar apologies. :p
- bearophile (8/13) Jan 13 2010 Yes, an over-usage of "auto" makes the code a bit less self-descriptive ...
- Leandro Lucarella (11/14) Jan 13 2010 Go allows var and an even shorter alternative, the := operator:
- dsimcha (24/38) Jan 13 2010 software engineering perspective as well, what does D's auto keyword buy...
- =?UTF-8?B?UGVsbGUgTcOlbnNzb24=?= (3/41) Jan 13 2010 This!
- Walter Bright (2/7) Jan 13 2010 I consider that a *major* advantage to auto.
- Andrei Alexandrescu (4/31) Jan 13 2010 "auto" is huge and has a lot to do with programming-at-large. Explicit
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/5) Jan 13 2010 C++0x has:
- Lutger (18/32) Jan 13 2010 A lot:
- retard (10/39) Jan 13 2010 The main benefit IMO is that you don't need to repeat things twice.
- Lutger (7/46) Jan 13 2010 Well I usually don't use names such as foo, bar and baz in production
- Nick Sabalausky (4/10) Jan 13 2010 Doesn't "Dim" (without an "As" clause) declare a variant rather than a
- Lutger (5/16) Jan 13 2010 What MS did was basically just drop Visual Basic, lot's of rusty coders
- Nick Sabalausky (6/28) Jan 13 2010 Well, from what I've seen from various co-workers, anyone who could only...
- Nick Sabalausky (18/24) Jan 13 2010 It makes it possible to have one variable's type match that of another
- downs (4/8) Jan 18 2010 If you're on gcc or a compiler that allows the typeof extension:
- Nick Sabalausky (3/12) Jan 18 2010 Heh, I've done something similar in generic D code :)
- Justin Johansson (13/24) Jan 26 2010 Thanks. I uncovered the keyword recently when I inadvertently typed
Happy New Year 2010 Everybody. Having resumed C++ nationality for the last few months, I kind of miss D's auto keyword. I am wondering, though, from an OO/polymorphism perspective, and UML and sound software engineering perspective as well, what does D's auto keyword buy you except (perhaps) laziness (in keystrokes)? Sure the auto variable decl allows the declared var to take on the static type (i.e. as inferred by the compiler), but the programmer still has to know (in subsequent method invocations applied to the auto var) just what methods are valid for the statically inferred var type being the subject of the auto decl. In some ways, as I said above, I miss "D auto" in C++; but then again, when I explicitly write the exact same type as the function return signature says, I feel more in control of my software design. In an ideal world, which in of course such utopia does not really exist, a pure OO model may well be that of single inheritance, and therefore all methods would, or could, be forced into a base class and hence, for object/polymorphic types D'auto keyword would not prove much advantage. (Pray, let's not get into fragile base class discussions.) At the end of the day, I'm not sure if D's auto keyword really helps to make my code more readable to others (alla programming-in-the-large) or if it just helps me with typing shortcuts (alla programming-in-the-small). btw. 20 years ago I thought the Forth language was fantastic. Then later I learned the difference between programming-in-the-small and programming-in-the-large. Of course, Forth still hold fond memories for me .. but today I'd still rather stick to C++. In writing this NG post, I was wondering about a subject line like "what's the best thing about D", but then my love/hate relationship with D's auto keyword really got me. btw. Do any other languages have an "auto" var idiom? I don't remember Scala having such (and it's really modern), though perhaps my memory lapses. Cheers again, Justin Johansson
Jan 13 2010
And sorry for saying btw twice in the same post. And sorry for posting this apology. JJ
Jan 13 2010
On Wed, 13 Jan 2010 17:52:41 +0100, Justin Johansson <no spam.com> wrote:And sorry for saying btw twice in the same post. And sorry for posting this apology. JJYou are not forgiven. Post moar apologies. :p -- Simen
Jan 13 2010
Justin Johansson:I am wondering, though, from an OO/polymorphism perspective, and UML and sound software engineering perspective as well, what does D's auto keyword buy you except (perhaps) laziness (in keystrokes)?In functional/generic code you sometimes end having very complex types (what comes out of a lazy chain of a lazy filtering of a lazy mapping of some lazy generator, for example), in such situation having a bit of type inference saves you a lot. And then you give this monster to other templated functions, that are able to work with any iterable (Range, in Phobos2), so there's no a huge type signature anywhere.In some ways, as I said above, I miss "D auto" in C++; but then again, when I explicitly write the exact same type as the function return signature says, I feel more in control of my software design.Yes, an over-usage of "auto" makes the code a bit less self-descriptive (this sometimes can be negative, so as with most features of a language you don't have to abuse it), but if you use dynamic languages you get used to that :-)btw. 20 years ago I thought the Forth language was fantastic. Then later I learned the difference between programming-in-the-small and programming-in-the-large.<The name of the recently developed Scala language means scalable language. It recognizes the need for both scaling up (what you are talking about here, that Java and especially Ada give you) and scaling down (what dynamic languages like Python sometimes give you). Both things are important, because a programmer has to write programs of all sizes, not just huge ones and not just small ones.I was wondering about a subject line like "what's the best thing about D",<I don't know. Probably the community around it. Walter is a bit like a bear sometimes, but still the community grown around his language is alive, despite its small size. Andrei has improved it some more.btw. Do any other languages have an "auto" var idiom? I don't remember Scala having such (and it's really modern), though perhaps my memory lapses.something similar, a local form of simple type inference. Bye, bearophile
Jan 13 2010
bearophile, el 13 de enero a las 12:01 me escribiste:Go allows var and an even shorter alternative, the := operator: var x = 1 <===> x := 1 -- Leandro Lucarella (AKA luca) http://llucax.com.ar/ ---------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------- Me encanta el éxito; por eso prefiero el estado de progreso constante, con la meta al frente y no atrás. -- Ricardo Vaporeso. Punta del Este, Enero de 1918.btw. Do any other languages have an "auto" var idiom? I don't remember Scala having such (and it's really modern), though perhaps my memory lapses.something similar, a local form of simple type inference.
Jan 13 2010
== Quote from Justin Johansson (no spam.com)'s articleHappy New Year 2010 Everybody. Having resumed C++ nationality for the last few months, I kind of miss D's autokeyword.I am wondering, though, from an OO/polymorphism perspective, and UML and soundsoftware engineering perspective as well, what does D's auto keyword buy you except (perhaps) laziness (in keystrokes)?Sure the auto variable decl allows the declared var to take on the static type(i.e. as inferred by the compiler), but the programmer still has to know (in subsequent method invocations applied to the auto var) just what methods are valid for the statically inferred var type being the subject of the auto decl.In some ways, as I said above, I miss "D auto" in C++; but then again, when Iexplicitly write the exact same type as the function return signature says, I feel more in control of my software design.In an ideal world, which in of course such utopia does not really exist, a pureOO model may well be that of single inheritance, and therefore all methods would, or could, be forced into a base class and hence, for object/polymorphic types D'auto keyword would not prove much advantage.(Pray, let's not get into fragile base class discussions.) At the end of the day, I'm not sure if D's auto keyword really helps to make mycode more readable to others (alla programming-in-the-large) or if it just helps me with typing shortcuts (alla programming-in-the-small).btw. 20 years ago I thought the Forth language was fantastic. Then later Ilearned the difference between programming-in-the-small and programming-in-the-large.Of course, Forth still hold fond memories for me .. but today I'd still ratherstick to C++.In writing this NG post, I was wondering about a subject line like "what's thebest thing about D", but then my love/hate relationship with D's auto keyword really got me.btw. Do any other languages have an "auto" var idiom? I don't remember Scalahaving such (and it's really modern), though perhaps my memory lapses.Cheers again, Justin JohanssonOne underappreciated thing auto gives is DRY for types. It makes it easier to change the type of some object in the place where it's initially decided, because those changes will automagically be propagated to everything that uses that object, as long as the new type supports the same compile-time interface as the old type.
Jan 13 2010
On 01/13/2010 06:19 PM, dsimcha wrote:== Quote from Justin Johansson (no spam.com)'s articleThis! Makes some refactoring tasks ridiculously smooth.Happy New Year 2010 Everybody. Having resumed C++ nationality for the last few months, I kind of miss D's autokeyword.I am wondering, though, from an OO/polymorphism perspective, and UML and soundsoftware engineering perspective as well, what does D's auto keyword buy you except (perhaps) laziness (in keystrokes)?Sure the auto variable decl allows the declared var to take on the static type(i.e. as inferred by the compiler), but the programmer still has to know (in subsequent method invocations applied to the auto var) just what methods are valid for the statically inferred var type being the subject of the auto decl.In some ways, as I said above, I miss "D auto" in C++; but then again, when Iexplicitly write the exact same type as the function return signature says, I feel more in control of my software design.In an ideal world, which in of course such utopia does not really exist, a pureOO model may well be that of single inheritance, and therefore all methods would, or could, be forced into a base class and hence, for object/polymorphic types D'auto keyword would not prove much advantage.(Pray, let's not get into fragile base class discussions.) At the end of the day, I'm not sure if D's auto keyword really helps to make mycode more readable to others (alla programming-in-the-large) or if it just helps me with typing shortcuts (alla programming-in-the-small).btw. 20 years ago I thought the Forth language was fantastic. Then later Ilearned the difference between programming-in-the-small and programming-in-the-large.Of course, Forth still hold fond memories for me .. but today I'd still ratherstick to C++.In writing this NG post, I was wondering about a subject line like "what's thebest thing about D", but then my love/hate relationship with D's auto keyword really got me.btw. Do any other languages have an "auto" var idiom? I don't remember Scalahaving such (and it's really modern), though perhaps my memory lapses.Cheers again, Justin JohanssonOne underappreciated thing auto gives is DRY for types. It makes it easier to change the type of some object in the place where it's initially decided, because those changes will automagically be propagated to everything that uses that object, as long as the new type supports the same compile-time interface as the old type.
Jan 13 2010
dsimcha wrote:One underappreciated thing auto gives is DRY for types. It makes it easier to change the type of some object in the place where it's initially decided, because those changes will automagically be propagated to everything that uses that object, as long as the new type supports the same compile-time interface as the old type.I consider that a *major* advantage to auto.
Jan 13 2010
Justin Johansson wrote:Happy New Year 2010 Everybody. Having resumed C++ nationality for the last few months, I kind of miss D's auto keyword. I am wondering, though, from an OO/polymorphism perspective, and UML and sound software engineering perspective as well, what does D's auto keyword buy you except (perhaps) laziness (in keystrokes)? Sure the auto variable decl allows the declared var to take on the static type (i.e. as inferred by the compiler), but the programmer still has to know (in subsequent method invocations applied to the auto var) just what methods are valid for the statically inferred var type being the subject of the auto decl. In some ways, as I said above, I miss "D auto" in C++; but then again, when I explicitly write the exact same type as the function return signature says, I feel more in control of my software design. In an ideal world, which in of course such utopia does not really exist, a pure OO model may well be that of single inheritance, and therefore all methods would, or could, be forced into a base class and hence, for object/polymorphic types D'auto keyword would not prove much advantage. (Pray, let's not get into fragile base class discussions.) At the end of the day, I'm not sure if D's auto keyword really helps to make my code more readable to others (alla programming-in-the-large) or if it just helps me with typing shortcuts (alla programming-in-the-small). btw. 20 years ago I thought the Forth language was fantastic. Then later I learned the difference between programming-in-the-small and programming-in-the-large. Of course, Forth still hold fond memories for me .. but today I'd still rather stick to C++. In writing this NG post, I was wondering about a subject line like "what's the best thing about D", but then my love/hate relationship with D's auto keyword really got me. btw. Do any other languages have an "auto" var idiom? I don't remember Scala having such (and it's really modern), though perhaps my memory lapses. Cheers again, Justin Johansson"auto" is huge and has a lot to do with programming-at-large. Explicit types are to generic code as bad as hardcoded magic numbers to regular code. Andrei
Jan 13 2010
Justin Johansson wrote:Do any other languages have an "auto" var idiom?C++0x has: http://www2.research.att.com/~bs/C++0xFAQ.html#auto Ali
Jan 13 2010
On 01/13/2010 05:49 PM, Justin Johansson wrote:Happy New Year 2010 Everybody. Having resumed C++ nationality for the last few months, I kind of miss D's auto keyword. I am wondering, though, from an OO/polymorphism perspective, and UML and sound software engineering perspective as well, what does D's auto keyword buy you except (perhaps) laziness (in keystrokes)?A lot: auto foo = bar(2); auto baz = grok(foo); No types to be seen here (aside from the literal 2) so you can: 1. rename the type whatever bar() returns without ripple effects through this code 2. substitute the type itself for another type that obeys the same structural interface. In a way, it's like not using magic numbers only applied to types :) The benefits are not limited too but more pronounced in templated code.Sure the auto variable decl allows the declared var to take on the static type (i.e. as inferred by the compiler), but the programmer still has to know (in subsequent method invocations applied to the auto var) just what methods are valid for the statically inferred var type being the subject of the auto decl. In some ways, as I said above, I miss "D auto" in C++; but then again, when I explicitly write the exact same type as the function return signature says, I feel more in control of my software design.Depends on how this is used I think, see also below.In an ideal world, which in of course such utopia does not really exist, a pure OO model may well be that of single inheritance, and therefore all methods would, or could, be forced into a base class and hence, for object/polymorphic types D'auto keyword would not prove much advantage. (Pray, let's not get into fragile base class discussions.) At the end of the day, I'm not sure if D's auto keyword really helps to make my code more readable to others (alla programming-in-the-large) or if it just helps me with typing shortcuts (alla programming-in-the-small). btw. 20 years ago I thought the Forth language was fantastic. Then later I learned the difference between programming-in-the-small and programming-in-the-large. Of course, Forth still hold fond memories for me .. but today I'd still rather stick to C++. In writing this NG post, I was wondering about a subject line like "what's the best thing about D", but then my love/hate relationship with D's auto keyword really got me. btw. Do any other languages have an "auto" var idiom? I don't remember Scala having such (and it's really modern), though perhaps my memory lapses. Cheers again, Justin JohanssonAll ML derived languages do this much more extensively and they even mean the same thing. The nice thing with those languages is that if you code in Visual Studio, you only have to hover above the variabele and the type pops up. Most IDE's have this btw, including descent, so that takes away the concern of having to remember the flow of types inferred.
Jan 13 2010
Wed, 13 Jan 2010 20:22:02 +0100, Lutger wrote:On 01/13/2010 05:49 PM, Justin Johansson wrote:The main benefit IMO is that you don't need to repeat things twice. sadklaskdjlaj<aslfkjals, asdkjasldjw<asldjalsdm, asdjslk>> foo = new sadklaskdjlaj<aslfkjals, asdkjasldjw<asldjalsdm, asdjslk>>(); // no thanks Repetition happens every time one uses advanced data structures like lists, hashes, tuples etc. In cases likeHappy New Year 2010 Everybody. Having resumed C++ nationality for the last few months, I kind of miss D's auto keyword. I am wondering, though, from an OO/polymorphism perspective, and UML and sound software engineering perspective as well, what does D's auto keyword buy you except (perhaps) laziness (in keystrokes)?A lot: No types to be seen here (aside from the literal 2) so you can: 1. rename the type whatever bar() returns without ripple effects through this code 2. substitute the type itself for another type that obeys the same structural interface. In a way, it's like not using magic numbers only applied to types :) The benefits are not limited too but more pronounced in templated code.auto foo = bar(2); auto baz = grok(foo);I would actually explicitly write to type some times to document things better.All ML derived languages do this much more extensively and they even mean the same thing. The nice thing with those languages is that if you code in Visual Studio, you only have to hover above the variabele and the type pops up. Most IDE's have this btw, including descent, so that takes away the concern of having to remember the flow of types inferred.A tru 1337 koder uses vim or emacs, both of which suck at semantic analysis :)
Jan 13 2010
On 01/13/2010 08:46 PM, retard wrote:Wed, 13 Jan 2010 20:22:02 +0100, Lutger wrote:Well I usually don't use names such as foo, bar and baz in production code ;) (I suck at naming and have recently started to make long names). I get your point but would consider that kind of repetition the least of the benefits, since there is no big downside here to copy & paste away.On 01/13/2010 05:49 PM, Justin Johansson wrote:The main benefit IMO is that you don't need to repeat things twice. sadklaskdjlaj<aslfkjals, asdkjasldjw<asldjalsdm, asdjslk>> foo = new sadklaskdjlaj<aslfkjals, asdkjasldjw<asldjalsdm, asdjslk>>(); // no thanks Repetition happens every time one uses advanced data structures like lists, hashes, tuples etc. In cases likeHappy New Year 2010 Everybody. Having resumed C++ nationality for the last few months, I kind of miss D's auto keyword. I am wondering, though, from an OO/polymorphism perspective, and UML and sound software engineering perspective as well, what does D's auto keyword buy you except (perhaps) laziness (in keystrokes)?A lot: No types to be seen here (aside from the literal 2) so you can: 1. rename the type whatever bar() returns without ripple effects through this code 2. substitute the type itself for another type that obeys the same structural interface. In a way, it's like not using magic numbers only applied to types :) The benefits are not limited too but more pronounced in templated code.auto foo = bar(2); auto baz = grok(foo);I would actually explicitly write to type some times to document things better.Then again, such a koder don't need no pedantic type declarations for reminders.All ML derived languages do this much more extensively and they even mean the same thing. The nice thing with those languages is that if you code in Visual Studio, you only have to hover above the variabele and the type pops up. Most IDE's have this btw, including descent, so that takes away the concern of having to remember the flow of types inferred.A tru 1337 koder uses vim or emacs, both of which suck at semantic analysis :)
Jan 13 2010
"Lutger" <lutger.blijdestijn gmail.com> wrote in message news:hil6gr$2hmn$1 digitalmars.com...All ML derived languages do this much more extensively and they even don't same thing. The nice thing with those languages is that if you code in Visual Studio, you only have to hover above the variabele and the type pops up. Most IDE's have this btw, including descent, so that takes away the concern of having to remember the flow of types inferred.Doesn't "Dim" (without an "As" clause) declare a variant rather than a compile-time-inferred static type? Or did that change in VB.NET?
Jan 13 2010
On 01/13/2010 10:22 PM, Nick Sabalausky wrote:"Lutger"<lutger.blijdestijn gmail.com> wrote in message news:hil6gr$2hmn$1 digitalmars.com...What MS did was basically just drop Visual Basic, lot's of rusty coders dying dynamic scripting language with a similar name. It is a different language altogether though, even D is more dynamic than VB.NET.All ML derived languages do this much more extensively and they even don't same thing. The nice thing with those languages is that if you code in Visual Studio, you only have to hover above the variabele and the type pops up. Most IDE's have this btw, including descent, so that takes away the concern of having to remember the flow of types inferred.Doesn't "Dim" (without an "As" clause) declare a variant rather than a compile-time-inferred static type? Or did that change in VB.NET?
Jan 13 2010
"Lutger" <lutger.blijdestijn gmail.com> wrote in message news:hilifj$7u7$1 digitalmars.com...On 01/13/2010 10:22 PM, Nick Sabalausky wrote:Well, from what I've seen from various co-workers, anyone who could only do VB wasn't much of a programmer to begin with."Lutger"<lutger.blijdestijn gmail.com> wrote in message news:hil6gr$2hmn$1 digitalmars.com...What MS did was basically just drop Visual Basic, lot's of rusty coders lost their job because of it.All ML derived languages do this much more extensively and they even don't the same thing. The nice thing with those languages is that if you code in Visual Studio, you only have to hover above the variabele and the type pops up. Most IDE's have this btw, including descent, so that takes away the concern of having to remember the flow of types inferred.Doesn't "Dim" (without an "As" clause) declare a variant rather than a compile-time-inferred static type? Or did that change in VB.NET?with a similar name. It is a different language altogether thoughOh yea, I knew that, I just didn't realize that "Dim" without "As" has become an "auto"-like thing.
Jan 13 2010
"Justin Johansson" <no spam.com> wrote in message news:hiktil$20ai$1 digitalmars.com...Happy New Year 2010 Everybody. Having resumed C++ nationality for the last few months, I kind of miss D's auto keyword. I am wondering, though, from an OO/polymorphism perspective, and UML and sound software engineering perspective as well, what does D's auto keyword buy you except (perhaps) laziness (in keystrokes)?It makes it possible to have one variable's type match that of another variable without having to update as many places when you want to change. Ex: --------------- class Foo { wchar x; void bar() { wchar[] y = x ~ "stuff"; wchar[] z = y ~ x; } } --------------- Now if I decide I want 'x' to be a 'char', I have to remember to change 'wchar'->'char' in all three declarations. But if 'y' and 'z' are declared with auto, all I have to do is change the declaration of 'x'. You could also get the same effect with typeof(), but that's more verbose and less DRY.
Jan 13 2010
Justin Johansson wrote:Happy New Year 2010 Everybody. Having resumed C++ nationality for the last few months, I kind of miss D's auto keyword.If you're on gcc or a compiler that allows the typeof extension: #define DECL(A, B) typeof(B) A = B :)
Jan 18 2010
"downs" <default_357-line yahoo.de> wrote in message news:hj1m0r$1k3l$1 digitalmars.com...Justin Johansson wrote:Heh, I've done something similar in generic D code :)Happy New Year 2010 Everybody. Having resumed C++ nationality for the last few months, I kind of miss D's auto keyword.If you're on gcc or a compiler that allows the typeof extension: #define DECL(A, B) typeof(B) A = B :)
Jan 18 2010
downs wrote:Justin Johansson wrote:Thanks. I uncovered the keyword recently when I inadvertently typed "typeof" instead of "typeid" and the editor in Eclipse CDT made no complaint. Consequently I searched both web and boost source for for usages of typeof and soon found that it was just an extension for gcc (and perhaps some others). It's a real pity for C++ that typeof keyword hasn't been standardized. Accordingly Boost doesn't use typeof. However, I noticed that they sometimes make some use of the "typedef-decl-nested-inside-a-class-or-struct-trick" to exact static type on occasions when typeof would otherwise suffice. Cheers Justin JohanssonHappy New Year 2010 Everybody. Having resumed C++ nationality for the last few months, I kind of miss D's auto keyword.If you're on gcc or a compiler that allows the typeof extension: #define DECL(A, B) typeof(B) A = B :)
Jan 26 2010