Re: 'auto' keyword

2023-03-12 Thread Ali Çehreli via Digitalmars-d-learn
On 3/12/23 06:07, DLearner wrote: > 1. As a shorthand to make the type of the variable being declared the > same as the type on the right hand side of an initial assignment. As Adam explained, D already has type inference without a special keyword. However, some places where 'auto' (or

Re: 'auto' keyword

2023-03-12 Thread kdevel via Digitalmars-d-learn
On Sunday, 12 March 2023 at 13:27:05 UTC, Adam D Ruppe wrote: [...] *any* storage class will work for type inference. [...] After heaving read [1] I immediately thought of this: void main () { deprecated i = 3; i = 4; } $ dmd test.d test.d(4): Deprecation: variable

Re: 'auto' keyword

2023-03-12 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 12 March 2023 at 15:31:07 UTC, Salih Dincer wrote: Moreover, `auto ref` or `ref auto` is needed in functions. That's because `ref` isn't part of the argument or return value's type, so it isn't covered by **type** inference. Instead, D has a totally separate feature for "`ref`

Re: 'auto' keyword

2023-03-12 Thread Salih Dincer via Digitalmars-d-learn
. The auto keyword is really helpful for shortening it. But in at least 2 cases (one of which is interfaces) it should help the compiler. For example, contrary to expected, it is dynamic array: ```d auto arr = [ 1, 2, 3 ]; ``` Moreover, `auto ref` or `ref auto` is needed in functions. SDB@79

Re: 'auto' keyword

2023-03-12 Thread Adam D Ruppe via Digitalmars-d-learn
On Sunday, 12 March 2023 at 13:07:58 UTC, DLearner wrote: Is it correct that this _single_ keyword is used to indicate _two_ quite different things: No, it only actually does #2 in your thing. The type is optional meaning *any* storage class will work for type inference. `auto` is not

'auto' keyword

2023-03-12 Thread DLearner via Digitalmars-d-learn
Is it correct that this _single_ keyword is used to indicate _two_ quite different things: 1. As a shorthand to make the type of the variable being declared the same as the type on the right hand side of an initial assignment. Example: ```auto A = 5;``` makes A an int. 2. To indicate

Re: auto keyword

2020-01-30 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jan 30, 2020 at 09:37:40AM +, Michael via Digitalmars-d-learn wrote: [...] > auto elements = buf.to!string.strip.split(" ").filter!(a => a != ""); > > That line strips white space from buf, splits it, removes empty > elements and returns an array of strings. At least I thought so. If

Re: auto keyword

2020-01-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, January 30, 2020 2:37:40 AM MST Michael via Digitalmars-d-learn wrote: > auto is surely a nice feature. Nonetheless I'd prefer to use > explicit types. So when reading a code and I see the auto keyword > I also have to find out what kind of type is meant. > > I have

auto keyword

2020-01-30 Thread Michael via Digitalmars-d-learn
auto is surely a nice feature. Nonetheless I'd prefer to use explicit types. So when reading a code and I see the auto keyword I also have to find out what kind of type is meant. I have a line of code that looks like this: auto elements = buf.to!string.strip.split(" &quo

Re: Auto keyword and when to use it

2018-08-22 Thread XavierAP via Digitalmars-d-learn
On Tuesday, 21 August 2018 at 21:37:00 UTC, QueenSvetlana wrote: I had a misunderstanding about the keyword auto because I wrongfully believed that it made the code like Python Exactly, you are thinking still like D is Python or also dynamically typed. :) You will get when compiling errors

Re: Auto keyword and when to use it

2018-08-21 Thread QueenSvetlana via Digitalmars-d-learn
On Tuesday, 21 August 2018 at 18:44:15 UTC, Jim Balter wrote: Python is not statically typed; D is. Why are you talking about Python? You asked whether D's auto is like C#'s var ... it is, but it doesn't have C#'s pointless restriction of not being allowed for non-local declarations. I think

Re: Auto keyword and when to use it

2018-08-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, August 21, 2018 12:22:42 PM MDT QueenSvetlana via Digitalmars-d- learn wrote: > 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

Re: Auto keyword and when to use it

2018-08-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, August 21, 2018 9:04:31 AM MDT Steven Schveighoffer via Digitalmars-d-learn wrote: > 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

Re: Auto keyword and when to use it

2018-08-21 Thread Jim Balter via Digitalmars-d-learn
On Tuesday, 21 August 2018 at 18:18:25 UTC, QueenSvetlana wrote: 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

Re: Auto keyword and when to use it

2018-08-21 Thread QueenSvetlana via Digitalmars-d-learn
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

Re: Auto keyword and when to use it

2018-08-21 Thread QueenSvetlana via Digitalmars-d-learn
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

Re: Auto keyword and when to use it

2018-08-21 Thread XavierAP via Digitalmars-d-learn
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

Re: Auto keyword and when to use it

2018-08-21 Thread Steven Schveighoffer via Digitalmars-d-learn
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

Re: Auto keyword and when to use it

2018-08-20 Thread Mike Parker via Digitalmars-d-learn
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

Re: Auto keyword and when to use it

2018-08-20 Thread JN via Digitalmars-d-learn
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

Re: Auto keyword and when to use it

2018-08-20 Thread Colin via Digitalmars-d-learn
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 }

Re: Auto keyword and when to use it

2018-08-20 Thread QueenSvetlana via Digitalmars-d-learn
Great! So I can't declare class level variables with auto, correct? only local method variables?

Re: Auto keyword and when to use it

2018-08-20 Thread JN via Digitalmars-d-learn
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

Auto keyword and when to use it

2018-08-20 Thread QueenSvetlana via Digitalmars-d-learn
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

Re: Auto keyword with const variable

2013-07-25 Thread Dicebot
On Wednesday, 24 July 2013 at 11:26:32 UTC, Mike Parker wrote: This is the exact behavior I would expect. I think of auto as this variable is going to be the same type as that variable. Since in is const int, then j also is going to be const int. If you want to copy n into a nonconst variable,

Auto keyword with const variable

2013-07-24 Thread Alex H
This code: void test(const int n) { auto j = n; j++; } Gives this error: cannot modify const expression j Is this considered a feature or a bug? I would assume most people wouldn't want new variables inheriting const.

Re: Auto keyword with const variable

2013-07-24 Thread bearophile
Alex H: void test(const int n) { auto j = n; j++; } Gives this error: cannot modify const expression j Is this considered a feature or a bug? I would assume most people wouldn't want new variables inheriting const. It's a bit annoying. I don't remember people discussing

Re: Auto keyword with const variable

2013-07-24 Thread MGW
On Wednesday, 24 July 2013 at 08:07:55 UTC, Alex H wrote: This code: void test(const int n) { auto j = n; j++; } Gives this error: cannot modify const expression j Is this considered a feature or a bug? I would assume most people wouldn't want new variables inheriting

Re: Auto keyword with const variable

2013-07-24 Thread dennis luehring
Am 24.07.2013 11:39, schrieb bearophile: Alex H: void test(const int n) { auto j = n; j++; } Gives this error: cannot modify const expression j Is this considered a feature or a bug? I would assume most people wouldn't want new variables inheriting const. It's a bit

Re: Auto keyword with const variable

2013-07-24 Thread bearophile
dennis luehring: and how would it look to preserve the const if auto would auto-rip it of? You could write: immutable j = n; For every default behavour you need a way to implement the other nicely :-) Currently for the problem of the OP you can use this: Unqual!(typeof(n)) j = n; Bye,

Re: Auto keyword with const variable

2013-07-24 Thread monarch_dodra
On Wednesday, 24 July 2013 at 10:01:14 UTC, bearophile wrote: dennis luehring: and how would it look to preserve the const if auto would auto-rip it of? You could write: immutable j = n; For every default behavour you need a way to implement the other nicely :-) Currently for the

Re: Auto keyword with const variable

2013-07-24 Thread Artur Skawina
On 07/24/13 12:09, monarch_dodra wrote: Keeping it to same type, 100% of the time seems like the best. No, it's a design bug. The head qualifier(s) should always be stripped when copying objects. Stripping is always fine (ie safe), not doing it just creates other problems, like the one in OP, or

Re: Auto keyword with const variable

2013-07-24 Thread monarch_dodra
On Wednesday, 24 July 2013 at 10:37:40 UTC, Artur Skawina wrote: On 07/24/13 12:09, monarch_dodra wrote: Keeping it to same type, 100% of the time seems like the best. No, it's a design bug. The head qualifier(s) should always be stripped when copying objects. Stripping is always fine (ie

Re: Auto keyword with const variable

2013-07-24 Thread Mike Parker
On Wednesday, 24 July 2013 at 08:07:55 UTC, Alex H wrote: This code: void test(const int n) { auto j = n; j++; } Gives this error: cannot modify const expression j Is this considered a feature or a bug? I would assume most people wouldn't want new variables inheriting

Re: Auto keyword with const variable

2013-07-24 Thread Jonathan M Davis
On Wednesday, July 24, 2013 10:07:54 Alex H wrote: This code: void test(const int n) { auto j = n; j++; } Gives this error: cannot modify const expression j Is this considered a feature or a bug? I would assume most people wouldn't want new variables inheriting const. The

Re: Auto keyword with const variable

2013-07-24 Thread bsd
On Wednesday, 24 July 2013 at 11:26:32 UTC, Mike Parker wrote: ... This is the exact behavior I would expect. I think of auto as this variable is going to be the same type as that variable. Since in is const int, then j also is going to be const int. If you want to copy n into a nonconst

Variable declaration programming style and the auto keyword

2013-07-04 Thread Jeremy DeHaan
I've seen a lot of code lately that uses the auto keyword when declaring variables, but for some reason I don't really care much for it. I'd like to make tutorials for a library I am working on, but I want to use D style. Does such a style exist? Is auto generally preferred when declaring

Re: Variable declaration programming style and the auto keyword

2013-07-04 Thread Namespace
On Thursday, 4 July 2013 at 20:00:18 UTC, Jeremy DeHaan wrote: I've seen a lot of code lately that uses the auto keyword when declaring variables, but for some reason I don't really care much for it. I'd like to make tutorials for a library I am working on, but I want to use D style. Does

Re: Variable declaration programming style and the auto keyword

2013-07-04 Thread Jonathan M Davis
On Thursday, July 04, 2013 21:54:22 Jeremy DeHaan wrote: I've seen a lot of code lately that uses the auto keyword when declaring variables, but for some reason I don't really care much for it. I'd like to make tutorials for a library I am working on, but I want to use D style. Does