# What is Result\<uint64,string\>

**URL:** <https://forums.fsharp.org/t/what-is-result-uint64-string/2128>\
**Category:** General\
**Tags:** beginners\
**Created:** [January 25, 2022, 2:25am UTC](https://forums.fsharp.org/t/what-is-result-uint64-string/2128 "2022-01-25T02:25:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonoke](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@jonoke](https://forums.fsharp.org/u/jonoke)\
**Post date:** [January 25, 2022, 2:25am UTC](https://forums.fsharp.org/t/what-is-result-uint64-string/2128/1 "2022-01-25T02:25:38Z")

</div>

I’m doing exercism, and in one problem they define the type of a function to be

```auto
Result<uint64,string>

```

I have no idea what that type is: could someone explain it, and or point me to somewhere in the docs that explains it.  
I looked at other peoples solutions and they are returning something like

```auto
Ok aNumber

```

I don’t get that either …

Jonathan.

---

<div class="post-metadata">

**Author:** ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)\
**Post date:** [January 25, 2022, 3:40pm UTC](https://forums.fsharp.org/t/what-is-result-uint64-string/2128/2 "2022-01-25T15:40:24Z")

</div>

Hi Jonathan, and welcome!  
`Result` in F# is a way to communicate errors or failure via the return type, as an alternative to just throwing an Exception. Let’s look at the type [definition](https://github.com/dotnet/fsharp/blob/c2efbc435e4e0347d8f1056e1ed440e67a7fb45f/src/fsharp/FSharp.Core/prim-types.fs#L3525) of `Result` from FSharp.Core:

```auto
    type Result<'T,'TError> = 
      | Ok of ResultValue:'T 
      | Error of ErrorValue:'TError

```

If that syntax isn’t familiar to you, it’s a “discriminated union,” and [this article](https://fsharpforfunandprofit.com/posts/discriminated-unions/) describes the concept pretty well. This type definition means that a value of type `Result<uint64, string>` could be either an `Ok` of some `uint64`, or an `Error` of some `string`. So:

```auto
let a : Result<uint64, string> = Ok 5UL // valid 
let b : Result<uint64, string> = Error "overflow" // also valid

```

If you have a Result, and you want to do something with it, the simplest thing to do is pattern match it:

```auto
match myResult with
| Ok i -> // do something here with i, which is a uint64
| Error e -> // do something here with e, which is a string

```

(Though the Result module has a few other helpful functions that can sometimes make working with Results more succinct, like `Result.map` and `Result.mapError`).  
There’s some debate in the F# community over when it’s more appropriate to use `Result` to handle errors, and when it’s more appropriate to throw Exceptions for errors instead. There’s a popular concept among F#ers called [“Railway Oriented Programming”](https://fsharpforfunandprofit.com/rop/) that heavily features `Result`.

---

<div class="post-metadata">

**Author:** ![bisen2](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/bisen2/32/1035_2.png) [@bisen2](https://forums.fsharp.org/u/bisen2)\
**Post date:** [January 27, 2022, 2:21pm UTC](https://forums.fsharp.org/t/what-is-result-uint64-string/2128/3 "2022-01-27T14:21:26Z")

</div>

The `Result<'t, 'u>`type is a [discriminated union](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/discriminated-unions) that is defined in the `FSharp.Core` library. It is defined as:

```auto

type Result<'t, 'u> =

  | Ok of 't

  | Error of 'u

```

The idea of returning this type is that a caller needs to explicitly handle both the case of a successful call and the case of an error. In your case, it is `Result<uint64, string>`, so if the function is successful it will return `Ok number` where `number` is a `uint64`. If the function fails, it will return `Error str` where `str` is a string, propbably explaining why the function failed. Say, for example, you are trying to write a function to perform safe division. You want to return a number if the division works, but if the denominator is zero, you want to return an error message. You could implement that like so:

```auto

let saveDivision (numerator: uint64) (denominator: uint64): Result<uint64, string> =

  if denominator = 0UL then

    Error "Division by zero"

  else

    Ok (numerator / denominator)

```

Then, a caller of that function can explicitly handle both cases:

```auto

let numerator = 10UL

let denominator = 0UL

let result = saveDivision numerator denominator

match result with

| Ok value -> printfn $"{numerator} / {denominator} = {value}"

| Error message -> printfn $"Error dividing {numerator} by {denominator}: {message}"

```
