The Result type makes it possible for a function to return a single value indicating all of the following things:
The Result type is a generic type containing two underlying types:
The Result type is also a [discriminated union][discriminated-union] with the following possible cases:
Ok <value> representing a successful resultError <reason> representing a failureThe following function demonstrates how to create a Result value:
let validateName (name: string) : Result<string, string> =
match name with
| null -> Error "Name not found."
| "" -> Error "Name is empty."
| _ -> Ok name
In this example, the Ok value is a string (the given name), and the Error value is also a string (the cause of the error, in human-readable form).
Result valueConsider the following type definition and function signature:
type FileOpenError =
| NotFound
| AccessDenied
| FileLocked
let openFile (filename: string) : Result<int, FileOpenError> =
Code that calls the openFile function can use pattern matching to handle the success and failure cases, as in the following example:
match openFile(filename) with
| Ok handle -> doSomethingWithFile(handle)
| Error NotFound -> printfn $"Error: file {filename} was not found."
| Error AccessDenied -> printfn $"Error: you do not have permission to open the file {filename}."
| Error FileLocked -> printfn $"Error: file {filename} is already in use."
Your task is to create a password checker. A password checker validates a user's proposed password to ensure that it meets a set of requirements defined by the organization that controls access to the given resource.
For this exercise, the password requirements are:
Your solution must use a Result to encapsulate the success or failure status.
For the success case, the Result must convey the validated password as a string.
For the failure case, the Result must convey the rule that was violated in the failure case.
For this exercise, the password checker will be simplistic -- it will indicate only when a single rule has been violated. A subsequent exercise will explore a more realistic password checker that can indicate when multiple rules have been violated at the same time.
checkPassword functionThe checkPassword function checks the given password against the aforementioned rules. On failure, it indicates the rule that was violated by encapsulating one of the PasswordRule values within the result value.
checkPassword "abcdefghij5#"
// => Error MissingUppercaseLetter
The getStatusMessage function returns a string containing a human-readable message indicating the meaning of the result returned from checkPassword.
getStatusMessage (Error MissingDigit)
// => "Error: does not have at least one digit"
Sign up to Exercism to learn and master F# with 18 concepts148 exercises, and real human mentoring, all for free.