digitalmars.D.learn - return val if
- Abby (8/8) Mar 22 2020 Is there a way to create a template that would do the same is glib
- Dennis (13/24) Mar 22 2020 Sounds a lot like:
- Steven Schveighoffer (5/22) Mar 22 2020 I think it's essentially the D way to do that, but it appears that the
- Mike Parker (23/32) Mar 22 2020 The documentation for the function reads:
- Mike Parker (7/13) Mar 22 2020 Heh, of course, as Dennis pointed out, that's essentialy
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; }
Mar 22 2020
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);
Mar 22 2020
On 3/22/20 3:04 PM, Dennis wrote:On Sunday, 22 March 2020 at 18:48:32 UTC, Abby wrote: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. -SteveIs 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); ```
Mar 22 2020
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); } ```
Mar 22 2020
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; ```
Mar 22 2020