TypeScript got never type which allows to define methods that never return.
I propose similar feature for C#.
Methods "returning" noreturn type must definitely throw or call other noreturn methods.
They can't have return statements in their body nor out parameters. At callsites they should be threated by the compiler similarly to throw statements.
It will require support from CLR. and that's a good thing as CLR could take advantage of that and optimize generated code around callsites to noreturns as it does now with throws.
private noreturn LogAndFailFast()
{
s_logger.Log(whatever_required);
Environment.FailFast();
}
static class ThrowHelper
{
public noreturn ThrowInvalidInputException(ObjectType type, long id)
{
throw new InvalidInputException(string.Format(InvalidInputFormatString, type, id));
}
}
TypeScript got
nevertype which allows to define methods that never return.I propose similar feature for C#.
Methods "returning"
noreturntype must definitely throw or call othernoreturnmethods.They can't have
returnstatements in their body noroutparameters. At callsites they should be threated by the compiler similarly tothrowstatements.It will require support from CLR. and that's a good thing as CLR could take advantage of that and optimize generated code around callsites to
noreturns as it does now withthrows.