Jump to page: 1 2
Thread overview
int x = auto
Mar 22, 2023
Ogi
Mar 22, 2023
Max Samukha
Mar 22, 2023
Nick Treleaven
Mar 22, 2023
Max Samukha
Mar 22, 2023
Max Samukha
Mar 22, 2023
Nick Treleaven
Mar 22, 2023
Dom Disc
Mar 22, 2023
John Colvin
Mar 24, 2023
Nick Treleaven
Mar 24, 2023
Nick Treleaven
Mar 22, 2023
Ali Çehreli
Mar 22, 2023
Sergey
Mar 23, 2023
Dom Disc
Mar 22, 2023
Ogi
March 22, 2023

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:

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:

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.
March 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.

March 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:

>

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.

March 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.

March 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 !()

March 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:

>

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.

March 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:

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:

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?

March 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:

>

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:

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:

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.

March 22, 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 it

That semicolon suggests you may have missed that the OP is talking about parameter lists. Or maybe not... :)

Ali

March 22, 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.

« First   ‹ Prev
1 2