Re: How to make a function that accepts optional struct but can accept struct literal too

2021-10-16 Thread Basile B. via Digitalmars-d-learn
On Friday, 15 October 2021 at 20:33:33 UTC, JN wrote: Is there some nice way of achieving something like this C99 code in D? [...] The [literal](https://www.ibm.com/docs/sr/xl-c-and-cpp-aix/13.1.0?topic=operators-compound-literal-expressions) in the C version creates an alloca too but it's h

Re: How to make a function that accepts optional struct but can accept struct literal too

2021-10-15 Thread Elronnd via Digitalmars-d-learn
On Friday, 15 October 2021 at 21:47:21 UTC, Paul Backus wrote: static global(alias value) = value; I fear there will be issues with reentrancy.

Re: How to make a function that accepts optional struct but can accept struct literal too

2021-10-15 Thread russhy via Digitalmars-d-learn
On Friday, 15 October 2021 at 21:47:21 UTC, Paul Backus wrote: On Friday, 15 October 2021 at 20:33:33 UTC, JN wrote: Is there some nice way of achieving something like this C99 code in D? ```c #include typedef struct { int x, y; } inputs_t; void foo(inputs_t* optional_inputs) { if (

Re: How to make a function that accepts optional struct but can accept struct literal too

2021-10-15 Thread Tejas via Digitalmars-d-learn
On Friday, 15 October 2021 at 21:19:35 UTC, jfondren wrote: On Friday, 15 October 2021 at 20:33:33 UTC, JN wrote: Is there some nice way of achieving something like this C99 code in D? option 1: use an intermediate lambda: ```d import std.stdio; struct inputs_t { int x, y; } // no ; need

Re: How to make a function that accepts optional struct but can accept struct literal too

2021-10-15 Thread Paul Backus via Digitalmars-d-learn
On Friday, 15 October 2021 at 20:33:33 UTC, JN wrote: Is there some nice way of achieving something like this C99 code in D? ```c #include typedef struct { int x, y; } inputs_t; void foo(inputs_t* optional_inputs) { if (!optional_inputs) { printf("0 0\n"); } else {

Re: How to make a function that accepts optional struct but can accept struct literal too

2021-10-15 Thread jfondren via Digitalmars-d-learn
On Friday, 15 October 2021 at 20:33:33 UTC, JN wrote: Is there some nice way of achieving something like this C99 code in D? option 1: use an intermediate lambda: ```d import std.stdio; struct inputs_t { int x, y; } // no ; needed here void foo(inputs_t* optional_inputs) { if (!optio

Re: How to make a function that accepts optional struct but can accept struct literal too

2021-10-15 Thread SomeGuy via Digitalmars-d-learn
You could use `Nullable` from the standard library to achieve something similar, but it isn't as simple/nice as your C99 compound literal example: ```D import std.stdio; import std.typecons; // https://dlang.org/phobos/std_typecons.html#Nullable struct inputs_t { int x, y; }; void foo(Nulla

How to make a function that accepts optional struct but can accept struct literal too

2021-10-15 Thread JN via Digitalmars-d-learn
Is there some nice way of achieving something like this C99 code in D? ```c #include typedef struct { int x, y; } inputs_t; void foo(inputs_t* optional_inputs) { if (!optional_inputs) { printf("0 0\n"); } else { printf("%d %d \n", optional_inputs->x, optional_inpu