Only want to say

ddcovery antoniocabreraperez at gmail.com
Tue Jan 12 14:41:27 UTC 2021


On Tuesday, 12 January 2021 at 13:52:48 UTC, Jacob Carlborg wrote:

> ...
> `assert` should be used to verify logical assumptions in your 
> program. To verify things in the environment, exceptions should 
> be used. You can drop the `in` and `out` contracts and use the 
> `enforce` function the same way as `assert` is used. It will 
> throw an exception if the condition doesn't hold.

Yes, totally agree

>
> Exceptions inheriting from the class `Exception` should be 
> thrown when there's an error related to the environment. I.e. a 
> missing file, failed to connect to a server and so on.
>
> Exceptions inheriting from the class `Error` should be thrown 
> when there's a logical error in the program, this is what 
> `assert` does. This includes accessing an array outside of its 
> bounds, failing to handle all cases in a `final switch` 
> statement and so on.

I didn't know about this distinction in D... thank you for the 
help!!! (I am throwing "error" instead "exception": I will be 
careful with this distinction)

>
> `assert` and contracts can be removed from the code, depending 
> on which compiler flags are being used. Exceptions will always 
> stay.
>
> In your case, there can be something like anti-virus software 
> which is running in the background and decides to remove your 
> files which the program is processing.

As I mentioned in the same post, I really use exceptions to 
manage IO operations (checking "previously" causes a race 
condition between check and operation:  it is the operation 
itself the one that must raise the exception)... my in/out 
assertions (as I mentioned in the same post) are really used for 
documentation purposes (I write assertions first and, then, the 
body code).

It is OK to remove at release time the contracts, because code is 
totally "contract" independent

Note:  I found that "invariant" for classes/structs can be 
removed using a compiler flag, but I didn't found information 
about removing the in/out... is it performed directly by compiler 
when generating a release?
>
> --
> /Jacob Carlborg

Thanks a lot Jacob




More information about the Digitalmars-d mailing list