According to the docs the failwith function returns an Exception
But the function signature is string -> 'a
Why is the signature not string -> Exception ?
According to the docs the failwith function returns an Exception
But the function signature is string -> 'a
Why is the signature not string -> Exception ?
Copyright © 2021 Jogjafile Inc.
The docs don't say that
failwithreturns anException. It says it generates an F# exception. The exception system is separate from the normal control flow of returning values. Hence the name, it's exceptional.Exceptions, when "thrown" (which is a less ambiguous term than "generated" as used in the docs, I think), will travel up the stack until encountering a
try ... withconstruct that handles this particular type of exception, or if not will terminate the program. See the F# docs on exception handling for details.failwithreturns'aso that it can be used anywhere, since'acan be inferred to be anything. It can pretend to return anything because it never actually returns at all, unlike most functions, it always throws an exception instead. If it had returnedExceptionit would only be able to be used in expressions that are expected to evaluate toException, which are exceptionally unusual since exceptions are usually thrown, not returned. For example, given:If
failwithhad returnedException, the compiler would complain here about anintbeing expected instead of anExceptionsince the first branch evaluates to anint. But sincefailwithreturns an'ainstead, it's inferred to be anintitself and everything is fine.