Skip to content

Proposal: noreturn methods #12199

@omariom

Description

@omariom

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));
   }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions