@noreturn property
Lars T. Kyllingstad
public at kyllingen.NOSPAMnet
Thu Oct 21 06:21:48 PDT 2010
On Thu, 21 Oct 2010 09:14:01 -0400, Steven Schveighoffer wrote:
> On Thu, 21 Oct 2010 08:52:35 -0400, Lars T. Kyllingstad
> <public at kyllingen.nospamnet> wrote:
>
>> On Thu, 21 Oct 2010 11:54:26 +0000, Iain Buclaw wrote:
>>
>>> A few standard library functions, such as 'abort' and 'exit', cannot
>>> return. However there is no way in DMD to let the compiler know about
>>> this. Currently in D2, you must either have a 'return' or 'assert(0)'
>>> statement at the end of a function body. It would be nice however if
>>> you can give hints to the compiler to let it know that a function is
>>> never going to return.
>>>
>>> Example:
>>>
>>> @noreturn void fatal()
>>> {
>>> print("Error");
>>> exit(1);
>>> }
>>>
>>> The 'noreturn' keyword would tell the compiler that 'fatal' cannot
>>> return, and can then optimise without regard to what would happen if
>>> 'fatal' ever did return. This should also allow fatal to be used
>>> instead of a return or assert statement.
>>>
>>> Example:
>>>
>>> int mycheck(int x)
>>> {
>>> if (x > 1)
>>> return OK;
>>> fatal();
>>> }
>>>
>>>
>>> Thoughts?
>>
>>
>> It would be useful for std.exception.enforce(), as you could end a
>> function with enforce(false).
>
> 1. It doesn't work that way. The function has to *never* return, no
> matter what the arguments.
Ah, of course. :)
> 2. assert(false) already does this.
Except that assert(false) throws an Error (or issues a HLT in release
mode), while enforce(false) throws an Exception.
But you are absolutely right, it won't work anyway, so I guess I'll just
have to suck it up and write 'throw new Exception'... :)
-Lars
More information about the Digitalmars-d
mailing list