Thread overview
return val if
Mar 22
Abby
Mar 22
Dennis
March 22
Is there a way to create a template that would do the same is glib

g_return_val_if_fail() (https://developer.gnome.org/glib/stable/glib-Warnings-and-Assertions.html#g-return-val-if-fail)

I was hoping something like this would work

template returnValIfFail(alias expr, alias val)
{
    if(expr) return val;
}
March 22
On Sunday, 22 March 2020 at 18:48:32 UTC, Abby wrote:
> Is there a way to create a template that would do the same is glib
>
> g_return_val_if_fail() (https://developer.gnome.org/glib/stable/glib-Warnings-and-Assertions.html#g-return-val-if-fail)

I'm not famililar with glib, but the description:

> If expr evaluates to FALSE, the current function should be considered to have undefined behaviour (a programmer error).

Sounds a lot like:

```
assert(expr);
```

> I was hoping something like this would work
>
> template returnValIfFail(alias expr, alias val)
> {
>     if(expr) return val;
> }

It would have to be a string mixin, you can't inject a return statement otherwise.
The thing is, why do you want that? It seems shorter to just write it out:

if(expr) return val;
debug if(expr) return val;
returnValIfFail!(expr, val);


March 22
On Sunday, 22 March 2020 at 18:48:32 UTC, Abby wrote:
> Is there a way to create a template that would do the same is glib
>
> g_return_val_if_fail() (https://developer.gnome.org/glib/stable/glib-Warnings-and-Assertions.html#g-return-val-if-fail)
>
> I was hoping something like this would work
>
> template returnValIfFail(alias expr, alias val)
> {
>     if(expr) return val;
> }

The documentation for the function reads:

"If expr evaluates to FALSE, the current function should be considered to have undefined behaviour (a programmer error). The only correct solution to such an error is to change the module that is calling the current function, so that it avoids this incorrect call."

Looks like the best approach in D would be to return val when expr is true and assert(0) otherwise. Also, you'll probably want the args to be available at runtime, else it would be a very restrictive template. Perhaps something like this?

```
T returnValIfFail(T)(bool expr, T val) {
    if(expr) return val;
    else assert(0);
}

void main() {
    import std.stdio : writeln;
    int y = 10;
    int x = returnValIfFail(y >= 10, 20);
    writeln(x);
}
```

March 22
On Sunday, 22 March 2020 at 19:04:40 UTC, Mike Parker wrote:

>
> ```
> T returnValIfFail(T)(bool expr, T val) {
>     if(expr) return val;
>     else assert(0);
> }

> ```

Heh, of course, as Dennis pointed out, that's essentialy assert(expr).

```
assert(expr);
x = val;
```
March 22
On 3/22/20 3:04 PM, Dennis wrote:
> On Sunday, 22 March 2020 at 18:48:32 UTC, Abby wrote:
>> Is there a way to create a template that would do the same is glib
>>
>> g_return_val_if_fail() (https://developer.gnome.org/glib/stable/glib-Warnings-and-Assertions.html#g-return-val-if-fail) 
>>
> 
> I'm not famililar with glib, but the description:
> 
>> If expr evaluates to FALSE, the current function should be considered to have undefined behaviour (a programmer error).
> 
> Sounds a lot like:
> 
> ```
> assert(expr);
> ```

I think it's essentially the D way to do that, but it appears that the behavior of glib is to return a value instead.

The exact behavior there is not reproducible in D AFAIK without mixins.

-Steve