digitalmars.D - int x = auto
- Ogi (32/32) Mar 22 2023 Suggestion: allow implicit default-initialization using `type x =
- Max Samukha (3/5) Mar 22 2023 No need for new syntax. It should just be `auto x = 0` or `auto x
- Nick Treleaven (9/14) Mar 22 2023 If the enum type inference DIP was accepted and extended to all
- Max Samukha (5/8) Mar 22 2023 That wouldn't work for default values that are not members of the
- Max Samukha (2/5) Mar 22 2023 *not need for !()
- Nick Treleaven (3/13) Mar 22 2023 Of course storage class type inference is still necessary for
- Dom Disc (14/30) Mar 22 2023 After:
- John Colvin (3/35) Mar 22 2023 No problem, if you submit a DIP and then go implement it in the
- Nick Treleaven (9/12) Mar 24 2023 There's a bugzilla issue preapproved by Walter with `auto ident =
- Nick Treleaven (4/9) Mar 24 2023 There is also this to back it up (as Walter's photo is not on the
- =?UTF-8?Q?Ali_=c3=87ehreli?= (5/10) Mar 22 2023 That semicolon suggests you may have missed that the OP is talking about...
- Ogi (3/4) Mar 22 2023 Just spotted a typo: it’s supposed to be `SomeStruct someStruct =
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
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
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: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.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
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
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
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:Of course storage class type inference is still necessary for those.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
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
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: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.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
On Wednesday, 22 March 2023 at 20:31:47 UTC, John Colvin wrote:No problem, if you submit a DIPThere'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.comand 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
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: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.orgNo problem, if you submit a DIPThere'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
Mar 24 2023
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 itThat semicolon suggests you may have missed that the OP is talking about parameter lists. Or maybe not... :) Ali
Mar 22 2023
On Wednesday, 22 March 2023 at 20:58:40 UTC, Ali Çehreli wrote:On 3/22/23 10:58, Dom Disc wrote:Btw it works in parameters list for the arrayAfter: ```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, weshould fix it That semicolon suggests you may have missed that the OP is talking about parameter lists. Or maybe not... :) Ali
Mar 22 2023
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
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