Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 20, 2018 Auto keyword and when to use it | ||||
---|---|---|---|---|
| ||||
I'm new to D programming, but have I have a background with Python. I'm struggling to understand what the auto keyword is for and it's appropriate uses. From research, it seems to share the same capabilities as the var keyword in C#. From the C# documentation, it states: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var ... variables that are declared at method scope can have an implicit "type" var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. Is the same true for auto? For example, if I have a class Person, I might have attributes such as FirstName, LastName which should obviously be strings but will D allow me to declare class level attributes with auto? C# doesn't allow this. |
August 20, 2018 Re: Auto keyword and when to use it | ||||
---|---|---|---|---|
| ||||
Posted in reply to QueenSvetlana | On Monday, 20 August 2018 at 17:24:19 UTC, QueenSvetlana wrote:
> I'm new to D programming, but have I have a background with Python.
>
> I'm struggling to understand what the auto keyword is for and it's appropriate uses. From research, it seems to share the same capabilities as the var keyword in C#. From the C# documentation, it states:
>
> https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var
>
> ... variables that are declared at method scope can have an implicit "type" var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.
>
> Is the same true for auto? For example, if I have a class Person, I might have attributes such as FirstName, LastName which should obviously be strings but will D allow me to declare class level attributes with auto?
>
> C# doesn't allow this.
It basically works like C#.
auto x = "hi";
will make x a string.
But if you later try:
x = 5;
it will throw an error, because x is a string. It's used to save you the typing of the type, it doesn't make the language dynamically typed.
|
August 20, 2018 Re: Auto keyword and when to use it | ||||
---|---|---|---|---|
| ||||
Posted in reply to JN | Great! So I can't declare class level variables with auto, correct? only local method variables? |
August 20, 2018 Re: Auto keyword and when to use it | ||||
---|---|---|---|---|
| ||||
Posted in reply to QueenSvetlana | On Monday, 20 August 2018 at 17:52:17 UTC, QueenSvetlana wrote:
> Great!
>
> So I can't declare class level variables with auto, correct? only local method variables?
You can use auto if you're setting the class level variable to a default.
class X {
auto i = 42; // i will be an int
}
|
August 20, 2018 Re: Auto keyword and when to use it | ||||
---|---|---|---|---|
| ||||
Posted in reply to QueenSvetlana | On Monday, 20 August 2018 at 17:52:17 UTC, QueenSvetlana wrote:
> Great!
>
> So I can't declare class level variables with auto, correct? only local method variables?
You can, globals, class members:
class Foo
{
auto bar = "hi";
}
Foo.bar will be of string type here, because "hi" is a string. What you can't do is:
class Foo
{
auto bar;
}
because now the compiler doesn't know what type 'bar' is supposed to be.
|
August 21, 2018 Re: Auto keyword and when to use it | ||||
---|---|---|---|---|
| ||||
Posted in reply to QueenSvetlana | On Monday, 20 August 2018 at 17:24:19 UTC, QueenSvetlana wrote:
>
> I'm struggling to understand what the auto keyword is for and it's appropriate uses. From research, it seems to share the same capabilities as the var keyword in C#.
auto is one of the most misunderstood understood features in D. By that I mean, everyone understands the effect of auto, but aren't always accurate in describing it.
In D, every variable must have a storage class. The automatic storage class is the default and is never specified in the declaration:
int x = 10;
Other storage classes are const, immutable, and shared. These are also type constructors, so they become part of the type:
const int y = 11; // type is const(int)
immutable int z = 12; // type is immutable(int)
shared int s = 13; // type is shared(int)
D allows the type to be dropped in declarations that include an initializer. In those cases, the type will be inferred:
const y = 11; // type is const(int)
immutable z = 12; // type is immutable(int)
shared s = 13; // type is shared(int)
You can also drop the type in declarations with automatic storage, but `x = 10;` is not allowed as a variable declaration. You must include at minimum a type or a storage class. That's where auto comes in:
auto x = 10; // type is int
So that's all it is. It's nothing special. It just means you're declaring a variable with the default storage class and want the compiler to infer the type.
So the question 'when should I use auto' is probably the wrong way to look at it. 'When should I use type inference' is a better way to frame it. And the answer to that is that there is no right answer.
I tend to use type inference liberally, almost always with const/immutbale locals, though I tend to use auto only when the type name is longer than four characters. For me, it's a nice way to save keystrokes. Some take a dim view of that approach and prefer to use it only when they actually require type inference. I mostly program alone, though, and I have a number of habits others may label 'bad', so I'm happy with my approach.
|
August 21, 2018 Re: Auto keyword and when to use it | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | On 8/20/18 9:15 PM, Mike Parker wrote:
> I tend to use type inference liberally, almost always with const/immutbale locals, though I tend to use auto only when the type name is longer than four characters. For me, it's a nice way to save keystrokes. Some take a dim view of that approach and prefer to use it only when they actually require type inference. I mostly program alone, though, and I have a number of habits others may label 'bad', so I'm happy with my approach.
I'm more extreme in this camp -- I use auto everywhere. Why? because at some point, I may change some type somewhere (oops, I should have wrote size_t instead of uint), and then I would have to go through and change all the places I put the concrete type if I hadn't used auto.
While using functions, I also can use auto and not have to worry about the type. I know kind of what it is (integer type, string type, range type, etc), and not care what the exact type is.
-Steve
|
August 21, 2018 Re: Auto keyword and when to use it | ||||
---|---|---|---|---|
| ||||
Posted in reply to QueenSvetlana | On Monday, 20 August 2018 at 17:52:17 UTC, QueenSvetlana wrote:
>
> So I can't declare class level variables with auto, correct? only local method variables?
One difference between D's auto and C#'s var or C++'s auto is that the latter languages allow automatically typed declarations only for local (method/function-scope) variables, and forbid them for class or struct member variables (aka fields); whereas D allows auto anywhere (even function/method return type! -- which C# and C++ allow as well but only case of anonymous methods/lambdas).
I'm in favor of the AAA ("Auto" Almost Always) paradigm, but as long as the type if obvious to a human reader. I don't favor them for numeric types for this reason (non obvious bitsize, signedness...) It's up to each programmer. Only if someone likes "Type x = new Type()" instead of "auto x = new Type()" I would say they're clearly wrong.
|
August 21, 2018 Re: Auto keyword and when to use it | ||||
---|---|---|---|---|
| ||||
Posted in reply to XavierAP | On Tuesday, 21 August 2018 at 16:15:32 UTC, XavierAP wrote:
>Only if someone
> likes "Type x = new Type()" instead of "auto x = new Type()" I would say they're clearly wrong.
As you stated it's up to the programmer to decided. I'm in favor of Type x = new Type() because when it comes to constructing a class it usually means more code to verify the types, for example:
class Person {
auto firstName;
auto lastName;
// constuctor to set first and last names
}
The compiler doesn't know know what firstName or lastName is supposed to be and a programmer might make the obvious assumption and use them as strings.
Doing this also means you have reject any type that isn't a string which means a private function to check the type that was pass to the constructor before initializing it. Where as if you declared it as a string to start of with, all you have to ensure is that it's not blank or contain illegal characters.
As the answer stated above doing what I showed in my example isn't allowed and this is where Python gets frustrating, because at any point the types could change. They introduced type hints, but it's not enforced, it just makes it more readable, you still have to write code to ensure the proper types were passed.
|
August 21, 2018 Re: Auto keyword and when to use it | ||||
---|---|---|---|---|
| ||||
Posted in reply to JN | On Monday, 20 August 2018 at 17:55:11 UTC, JN wrote:
> class Foo
> {
> auto bar;
> }
>
> because now the compiler doesn't know what type 'bar' is supposed to be.
Just to clarify, even if I set bar in the constructor, I can't declare it with auto first, correct? I would have to declare a specific type?
|
Copyright © 1999-2021 by the D Language Foundation