www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - int x = auto

reply Ogi <ogion.art gmail.com> writes:
Suggestion: allow implicit default-initialization using `type x = 
auto` syntax. This is the same as `type x = type.init` and `type 
x = type()`.

This will be especially handy for functions with optional 
parameters.

Before:
```D
void fun(
         int x = 0,
         double x = double.nan,
         bool flag = false,
         string str = "",
         void* ptr = null,
         SomeStruct = SomeStruct(),
         int[4] arr = [0, 0, 0, 0])
```

After:
```D
void fun(
         int x = auto,
         double f = auto,
         bool flag = auto,
         string str = auto,
         void* ptr = auto,
         SomeStruct = auto,
         int[4] arr = auto)
```

Possible alternatives:
* `int x = init`: most obvious, but this would require making 
`init` a reserved keyword.
* `int x = default`: more clear than `auto` but also quite longer.
* `int x = {}`: incompatible with D syntax.
Mar 22 2023
next sibling parent reply Max Samukha <maxsamukha gmail.com> writes:
On Wednesday, 22 March 2023 at 08:31:46 UTC, Ogi wrote:

 void fun(
         int x = auto,
No need for new syntax. It should just be `auto x = 0` or `auto x = int.init` like any other variable declaration.
Mar 22 2023
parent reply Nick Treleaven <nick geany.org> writes:
On Wednesday, 22 March 2023 at 08:51:27 UTC, Max Samukha wrote:
 On Wednesday, 22 March 2023 at 08:31:46 UTC, Ogi wrote:

 void fun(
         int x = auto,
No need for new syntax. It should just be `auto x = 0` or `auto x = int.init` like any other variable declaration.
If the enum type inference DIP was accepted and extended to all types, you could write: int x = $init; (Or with some other sigil, ?init maybe). https://github.com/dlang/DIPs/blob/master/DIPs/DIP1044.md The advantage is it's easier to read function signatures because parameters without default arguments have the parameter type on the left, not the right. So it's more consistent.
Mar 22 2023
parent reply Max Samukha <maxsamukha gmail.com> writes:
On Wednesday, 22 March 2023 at 12:49:23 UTC, Nick Treleaven wrote:

 If the enum type inference DIP was accepted and extended to all 
 types, you could write:

 int x = $init;
That wouldn't work for default values that are not members of the parameter type - `auto x = 0, auto x = someUnfortunateStructFactory!()()`, etc. Default values are initializers. Type inference should just work for them.
Mar 22 2023
next sibling parent Max Samukha <maxsamukha gmail.com> writes:
On Wednesday, 22 March 2023 at 13:41:44 UTC, Max Samukha wrote:
 On Wednesday, 22 March 2023 at 12:49:23 UTC, Nick Treleaven 
 wrote:
 `someUnfortunateStructFactory!()()`
*not need for !()
Mar 22 2023
prev sibling parent Nick Treleaven <nick geany.org> writes:
On Wednesday, 22 March 2023 at 13:41:44 UTC, Max Samukha wrote:
 On Wednesday, 22 March 2023 at 12:49:23 UTC, Nick Treleaven 
 wrote:

 If the enum type inference DIP was accepted and extended to 
 all types, you could write:

 int x = $init;
That wouldn't work for default values that are not members of the parameter type - `auto x = 0, auto x = someUnfortunateStructFactory!()()`, etc. Default values are initializers. Type inference should just work for them.
Of course storage class type inference is still necessary for those.
Mar 22 2023
prev sibling next sibling parent reply Dom Disc <dominikus scherkl.de> writes:
On Wednesday, 22 March 2023 at 08:31:46 UTC, Ogi wrote:
 Suggestion: allow implicit default-initialization using `type x 
 = auto` syntax. This is the same as `type x = type.init` and 
 `type x = type()`.

 This will be especially handy for functions with optional 
 parameters.

 Before:
 ```D
 void fun(
         int x = 0,
         double y = double.nan,
         bool flag = false,
         string str = "",
         void* ptr = null,
         SomeStruct t = SomeStruct(),
         int[4] arr = [0, 0, 0, 0])
 ```
After: ```D void fun( auto x = int.init, auto y = double.init, auto flag = bool.init, auto str = string.init, auto ptr = void*.init, // ok, not sure if this works auto t = SomeStruct.init, auto arr = int[4].init; // if this doesn't work, we should fix it ``` So, where is the problem?
Mar 22 2023
next sibling parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Wednesday, 22 March 2023 at 17:58:38 UTC, Dom Disc wrote:
 On Wednesday, 22 March 2023 at 08:31:46 UTC, Ogi wrote:
 Suggestion: allow implicit default-initialization using `type 
 x = auto` syntax. This is the same as `type x = type.init` and 
 `type x = type()`.

 This will be especially handy for functions with optional 
 parameters.

 Before:
 ```D
 void fun(
         int x = 0,
         double y = double.nan,
         bool flag = false,
         string str = "",
         void* ptr = null,
         SomeStruct t = SomeStruct(),
         int[4] arr = [0, 0, 0, 0])
 ```
After: ```D void fun( auto x = int.init, auto y = double.init, auto flag = bool.init, auto str = string.init, auto ptr = void*.init, // ok, not sure if this works auto t = SomeStruct.init, auto arr = int[4].init; // if this doesn't work, we should fix it ``` So, where is the problem?
No problem, if you submit a DIP and then go implement it in the compiler, because afaict that doesn’t work at all at the moment.
Mar 22 2023
parent reply Nick Treleaven <nick geany.org> writes:
On Wednesday, 22 March 2023 at 20:31:47 UTC, John Colvin wrote:
 No problem, if you submit a DIP
There's a bugzilla issue preapproved by Walter with `auto ident = exp` syntax: https://forum.dlang.org/post/mailman.493.1487193825.31550.digitalmars-d-bugs puremagic.com
 and then go implement it in the compiler, because afaict that 
 doesn’t work at all at the moment.
There was already a pull request that was rejected before the preapproved status: https://github.com/dlang/dmd/pull/2270 It allowed `ident = exp` and `storage_class ident = exp`. The former should be disallowed - use `auto` instead.
Mar 24 2023
parent Nick Treleaven <nick geany.org> writes:
On Friday, 24 March 2023 at 13:50:56 UTC, Nick Treleaven wrote:
 On Wednesday, 22 March 2023 at 20:31:47 UTC, John Colvin wrote:
 No problem, if you submit a DIP
There's a bugzilla issue preapproved by Walter with `auto ident = exp` syntax: https://forum.dlang.org/post/mailman.493.1487193825.31550.digitalmars-d-bugs puremagic.com
There is also this to back it up (as Walter's photo is not on the above post): https://forum.dlang.org/post/1712dc6c-90c2-36d2-5d27-09cfafe25e75 erdani.org
Mar 24 2023
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 3/22/23 10:58, Dom Disc wrote:

 After:
 ```D
 void fun(
Note: There is no closing parenthesis and a curly brace up there. :)
     auto t = SomeStruct.init,
     auto arr = int[4].init; // if this doesn't work, we should fix it
That semicolon suggests you may have missed that the OP is talking about parameter lists. Or maybe not... :) Ali
Mar 22 2023
next sibling parent Sergey <kornburn yandex.ru> writes:
On Wednesday, 22 March 2023 at 20:58:40 UTC, Ali Çehreli wrote:
 On 3/22/23 10:58, Dom Disc wrote:

 After:
 ```D
 void fun(
Note: There is no closing parenthesis and a curly brace up there. :)
     auto t = SomeStruct.init,
     auto arr = int[4].init; // if this doesn't work, we
should fix it That semicolon suggests you may have missed that the OP is talking about parameter lists. Or maybe not... :) Ali
Btw it works in parameters list for the array
Mar 22 2023
prev sibling parent Dom Disc <dominikus scherkl.de> writes:
On Wednesday, 22 March 2023 at 20:58:40 UTC, Ali Çehreli wrote:
 Note: There is no closing parenthesis and a curly brace up 
 there. :)
 That semicolon suggests you may have missed that the OP is 
 talking about parameter lists. Or maybe not... :)
Ah, ok. thanks. I've missed that indeed. So I think we should allow "foo(auto x = 5)" in general. The type of default-parameters should be as unambiguous as the type of variable declarations (with init type). At least that wouldn't need any new syntax and is self-explaining. Also it should be very easy to implement.
Mar 23 2023
prev sibling parent Ogi <ogion.art gmail.com> writes:
On Wednesday, 22 March 2023 at 08:31:46 UTC, Ogi wrote:
 ...
Just spotted a typo: it’s supposed to be `SomeStruct someStruct = SomeStruct()` and `SomeStruct someStruct = auto` respectfully.
Mar 22 2023