Re: "if not" condition check (for data validation)
On Thursday, 18 June 2020 at 17:39:44 UTC, Denis wrote: I should add that this one made me laugh though, giving flashbacks to that horrible "not speak" of the early 90s: if ( configfile.isFile.not ) ... LOL Approve Yoda does.
Re: "if not" condition check (for data validation)
On Thursday, 18 June 2020 at 17:57:49 UTC, Ali Çehreli wrote: Here is an earlier experiment of nested templates, which may be useful in this case. : I think you should be able to pass callables as 'alias' template arguments Sounds good. This gives me an opportunity to learn how nested templates can be used. `alias` in this context is also new, so I'll dig into that too. Thanks!
Re: "if not" condition check (for data validation)
On 6/18/20 5:13 AM, Denis wrote: > Templates offer a clean syntax Here is an earlier experiment of nested templates, which may be useful in this case. This is unrelated to your problem but the syntax can be pretty readable with templates: // If there are template arguments, then the result is the first of // them. If there is no argument, then the result is the argument // of 'otherwise'. template FirstOf(T...) { template otherwise(alias D) { static if (T.length != 0) { enum otherwise = T[0]; } else { enum otherwise = D; } } } unittest { static assert (FirstOf!(1.5, "hello").otherwise!100 == 1.5); static assert (FirstOf!().otherwise!42 == 42); } auto foo(Args...)() { auto temp = FirstOf!Args.otherwise!1.5; // ... return temp + 0.5; } unittest { assert(foo!(10, int[])() == 10.5); assert(foo() == 2.0); } void main() { } I think you should be able to pass callables as 'alias' template arguments but I couldn't put much thought into it. Ali
Re: "if not" condition check (for data validation)
On Thursday, 18 June 2020 at 12:50:35 UTC, Stanislav Blinov wrote: No, there isn't a way to write an operator. OK, first choice eliminated. On Thursday, 18 June 2020 at 13:57:39 UTC, Dukc wrote: No reason to use templates here pragma(inline, true) auto not(bool cond) { return !cond(); } I like it. The inline pragma eliminates the extra overhead of the function call, which was another objective. (And it introduces me to D's pragmas too.) Personally, I find this: if ( not( abra && cadabra )) ... to be more clear than: if ( !( abra && cadabra )) ... I guess it depends on what one is used to. I do recognize that this would be non-standard for D, but I'm still going to use it because I find it more readabile. I should add that this one made me laugh though, giving flashbacks to that horrible "not speak" of the early 90s: if ( configfile.isFile.not ) ... LOL I've learned multiple things from this posting. Thank you all for sharing your suggestions. Denis
Re: "if not" condition check (for data validation)
On Thursday, 18 June 2020 at 13:58:33 UTC, Dukc wrote: On Thursday, 18 June 2020 at 13:57:39 UTC, Dukc wrote: if (not!(abra && cadabra)) ... if (not(abra && cadabra)) ... Which is a quite a complicated way to write if (!(abra && cadabra)) ...
Re: "if not" condition check (for data validation)
On Thursday, 18 June 2020 at 13:57:39 UTC, Dukc wrote: No reason to use templates here Pff. Me no think straight. -.-
Re: "if not" condition check (for data validation)
On Thursday, 18 June 2020 at 12:50:35 UTC, Stanislav Blinov wrote: auto not(alias cond)() { return !cond(); } if (not!(() => abra && cadabra)) ... but that is indeed even less readable. No reason to use templates here ``` pragma(inline, true) auto not(bool cond) { return !cond(); } if (not!(abra && cadabra)) ... //same as above if (abra.not || cadabra.not) ... ```
Re: "if not" condition check (for data validation)
On Thursday, 18 June 2020 at 13:57:39 UTC, Dukc wrote: if (not!(abra && cadabra)) ... if (not(abra && cadabra)) ...
Re: "if not" condition check (for data validation)
On Thursday, 18 June 2020 at 12:13:21 UTC, Denis wrote: THE ESSENTIAL QUESTION Is there a way to write an `unless` operator that would allow the condition to be expressed in an affirmative sense? It would be used like `if`, i.e. something like: unless ( ) { ; // Or even: continue; } Templates offer a clean syntax, but I can't come up with a way to use them for a conditional operator. Mixins are flexibile, but I suspect the result would not be very readabile (that is, less readable even than "if ( ! (..." ). I was hoping that some feature, or combination of features, in D might allow this to be achieved. No, there isn't a way to write an operator. And yes, with templates you could do something like auto not(alias cond)() { return !cond(); } if (not!(() => abra && cadabra)) ... but that is indeed even less readable.
Re: "if not" condition check (for data validation)
Let me clarify the requirements and rephrase the question -- REQUIREMENTS `assert`, `enforce` and the `unless` function I wrote (above) all allow the condition to be expressed in an affirmative sense, and they also achieve the goal of readability. These are the initial requirements. `unless` improves on `assert` and `enforce` by allowing a custom action to be specified. This might be to write an error message and exit (like `assert` and `enforce`). But it could also be to write a warning message (to console or a log file) but not exit. So the next requirement is to be able to specify a custom action. Unfortunately a function, like the one I wrote, doesn't work because it doesn't allow actions that would skip or repeat some code (`continue`, `goto`, etc) to be specified as the second argument. It also doesn't allow code to be skipped or repeated after executing the function in the second argument. (That is, after writing a warning message or log notice, continue processing at some point.) A conditional operator like `if` is needed, not a function. THE ESSENTIAL QUESTION Is there a way to write an `unless` operator that would allow the condition to be expressed in an affirmative sense? It would be used like `if`, i.e. something like: unless ( ) { ; // Or even: continue; } Templates offer a clean syntax, but I can't come up with a way to use them for a conditional operator. Mixins are flexibile, but I suspect the result would not be very readabile (that is, less readable even than "if ( ! (..." ). I was hoping that some feature, or combination of features, in D might allow this to be achieved.
Re: "if not" condition check (for data validation)
On 6/17/20 4:46 PM, Denis wrote:> Is there a cleaner way to implement an "if not" condition check? >if ( configfile.isFile && configfile.extension == ".conf", message ) { } >else if (isConfigFile(name)) { // ... } else { // ... } The following is suitable in many cases: enforce(isConfigFile(name), format!"%s is not a config file"(name)); // ... I shortened that in many occasions: enforceConfigFile(name); // ... Of course, depending on the situation it is assert() or assertConfigFile(). Ali
Re: "if not" condition check (for data validation)
On Thursday, 18 June 2020 at 00:43:40 UTC, Stanislav Blinov wrote: if( ! (configfile.isFile && configfile.extension == ".conf") ) ? That does indeed clean up the compound logic. One is still left with: if( !( if( ! if( !( if( ! : I was hoping to get away from all the `not`s too.
Re: "if not" condition check (for data validation)
On Wednesday, 17 June 2020 at 23:46:54 UTC, Denis wrote: `if` is not a good substitute, because it works in the opposite sense, often requiring lots of `not`s. As a trivial example: assert( configfile.isFile && configfile.extension == ".conf" ) -vs- if ( !configfile.isFile || configfile.extension != ".conf" ) A BETTER SOLUTION ??? I haven't been able to come up with another option that is more efficient yet doesn't sacrifice readability. I would welcome suggestions. Thanks in advance. Denis if( ! (configfile.isFile && configfile.extension == ".conf") ) ?