What Is the Never Return Type

What is never return type in PHP 8.1

never type declaration introduced to be used as a return type hint for functions that never have return statement neither implicit nor explicit. And must be terminated by throwing an exception or exiting using exit/die functions.

function redirect(string $uri): never 
{
header('Location: ' . $uri);
exit();
}

Here redirect is called a never-returning function, because:

1) It has no return statement defined explicitly.

function redirect(string $uri): never 
{
exit();
return 'something';
}

Will prodcue:

PHP Fatal error: A never-returning function must not return

2) It has no return statement defined implicitly.

function redirect(string $uri): never
{
if (false) {
header('Location: ' . $uri);
exit();
}
}

Since the condition here is never satisfied, the execution jump over the if statement returning an implicit NULL which will result in:

PHP Fatal error: Uncaught TypeError: redirect(): never-returning function must not implicitly return

3) It ends it's execution with an exit function

void vs never

  • void can have return; but never can't.
  • never enforces that a function throws or is terminated with exit/die but void does
    not.
  • never is a subtype of every other type in PHP’s type system, including void (this allows return type covariance).
  • Both don't allow returning a value.
  • Both can't be used as a parameter or a property type.
  • Both can't be used as a return type of an arrow function.
  • Both can only be used as a standalone type (no union or intersection is allowed).

When to choose void over never and vice versa?

You should declare a function return type void when you expect PHP to execute the next statement after the function call. And you should declare it never when you don't expect PHP to execute the next statement after that function call.

Is there any difference between never and void return type in swift? what is the use of never return type in practically?

Void

A return type that doesn’t return a value.

Never

Something that will never return.

Void vs Never

  • Implementation: Void is a tuple, Never is an empty enum.
  • Never informs the compiler that there is no need to return a value.
  • Never is used in methods that will unconditionally throw an error or crash the system.

Understanding the never type in TypeScript 2

Never is information that this particular part shouldn't be reachable. For example in this code

function do(): never {
while (true) {}
}

you have an infinite loop and we don't want to iterate infinite loop. Simply as that.

But a real question is how can it be useful for us?
It might be helpful for instance while creating more advanced types to point what they are not

for example, let's declare our own NonNullable type:

type NonNullable<T> = T extends null | undefined ? never : T;

Here we are checking if T is null or undefined. If it is then we are pointing that it should never happen. Then while using this type:

let value: NonNullable<string>;
value = "Test";
value = null; // error

Error missing when inferring `never` return type from anonymous function

This is a limitation of the TypeScript compiler. It does code path analysis before type inference, and the second example doesn't work because even though it's strongly typed, it's not explicitly typed.

I wish this was better documented, but you can read through this pull request for more information about this limitation.

If you don't like explicitly providing a type annotation as shown in your first example, you can also create an overload that defines the throw condition statically and explicitly.

function willThrowException (message: string): never;
function willThrowException (message: string) {
throw new Error(message);
}

Looking at the above, it's wild that the overload is explicit enough and the return type isn't. Perhaps this is worth a discussion on TypeScript's Discord or GitHub issues.

Why does Swift's typechecking system allow a function that returns a type to not return anything?

Because fatalError returns Never. From the documentation, Never is,

The return type of functions that do not return normally, that is, a type with no values.

Through compiler magic, Swift understands that fatalError "does not return normally", namely that it crashes the app. This is why it does not complain about square not returning an Int.

If a path of your function call a function that "does not return normally", that path does not return normally either. "Does not return normally" means that the caller won't be able to use the return value, so there is no point in enforcing the "you must return a value" rule in that path of your function.

Return type method Return value of the method is never used problem

I assume what you mean is not really an error, but rather a warning from your IDE. Your IDE basically wants to tell you, that you return a value with your method (in your case an int value), but never use this value to work with it.

Therefore, the IDE suggests you improve your code, as if you never need the value you could also just return a void value. Therefore you have two ways to solve this issue:

a) In the method from where you call countMultiple(String[] s) you can work with that value, e.g. by simply logging it.

b) If you don't need the value in the caller method, you can return void: public static void countMultiple(String[] s)

For case b, think of removing return counter; from the method as well.

What is the difference between never and void in typescript?

In imperative languages, void can be thought of as a type containing a single value. Such languages do not provide a means to construct or consume this value, but a void function can be thought of as returning this trivial value.

In contrast never is a type containing no values, which means that a function with this return type can never return normally at all. This means either throwing an exception or failing to terminate.

Type hint that a function never returns

Even though “PEP 484 — Type Hints” standard mentioned both in question and in the answer yet nobody quotes its section: The NoReturn type that covers your question.

Quote:

The typing module provides a special type NoReturn to annotate functions that never return normally. For example, a function that unconditionally raises an exception:

from typing import NoReturn

def stop() -> NoReturn:
raise RuntimeError('no way')

The section also provides examples of the wrong usages. Though it doesn’t cover functions with an endless loop, in type theory they both equally satisfy never returns meaning expressed by that special type.



Related Topics



Leave a reply



Submit