Flagging special conditions on return from a function call

Denis noreply at noserver.lan
Tue Jun 23 16:14:20 UTC 2020


Perhaps this thread would have been better titled "Returning a 
value and a status", and the question phrased as "What are your 
preferred techniques?".

I'm planning to port some of my programs to D, so I'd like to 
standardize on one or two techniques for handling this very 
common situation, in a way that suits the language. I'd 
appreciate hearing from others about what you do.

---

On Tuesday, 23 June 2020 at 06:52:29 UTC, Simen Kjærås wrote:
> On Tuesday, 23 June 2020 at 04:01:45 UTC, Denis wrote:

[...]

>> (4) Set the function return value normally, and put the flag 
>> in an "out" variable passed as an argument to the function
>> (5) Return the flag, and put the value in an "out" variable 
>> passed to the function (i.e. the reverse of #4)
>
> Both of these happen. I don't know which is more common. In C# 
> these are probably the most common way (except for exceptions) 
> to signal these cases.

Good to know. Both of these techniques (or something similar) are 
used in Perl too. The choice seemed to depend on which one the 
author thought would be more useful as the return.

[...]

> by presenting an interface that only compiles when both cases 
> are covered, like fun().match((T t) => t, () => Error()).

A complete solution wrapped in a tidy package -- I like it.

Thanks for sharing.


More information about the Digitalmars-d-learn mailing list