"if not" condition check (for data validation)

Denis noreply at noserver.lan
Thu Jun 18 12:13:21 UTC 2020


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 ( <condition> ) {
      <handle it>;		// Or even: <ignore it>
      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.


More information about the Digitalmars-d-learn mailing list