Generic memory allocation in D

Dennis dkorpel at gmail.com
Mon Mar 4 11:46:10 UTC 2024


On Monday, 4 March 2024 at 09:53:17 UTC, Walter Bright wrote:
> It means I don't have to use ! in:
>
>     if (foo())
>     {
>         error();
>         return;
>     }

It's confusing how sometimes 0 is used for success, and other 
times 0 is used for failure.
For a boolean it makes more sense for `true` to be success, so 
the Windows API uses it, as you can see in the PR:

```
if (!CloseHandle(h))
     return Failure;
```

This reads as "if I could not close the handle, return failure".

There's often only 1 success state and many error states however. 
In that case you might want to return an int with multiple error 
codes, but there's only one 0. This code could also make sense:

```
if (auto errorCode = CloseHandle(h))
     return errorCode;
```

Now the error case is 'truthy'. Unfortunately, I don't think D 
can help differentiate between the two cases to prevent mistakes 
when using APIs returning success / error states.


More information about the Digitalmars-d mailing list