Thomas
Thomas

Reputation: 12107

using Result<> without a type in F#

I would like to do something like this:

let a x : Result<_, string> =
    match x with
    | 1 -> Ok
    | _ -> Error "not 1"

this will not compile.

Is there a way to not specify an ok type and return nothing?

Upvotes: 2

Views: 270

Answers (1)

JLRishe
JLRishe

Reputation: 101680

Not an experienced F# coder, but my understanding is that the way of indicating "no return value" is to use a Unit.

This compiles. Would it suffice for what you're doing?

let a x =
    match x with
    | 1 -> Ok ()
    | _ -> Error "not 1"

Or with an explicit return type:

let a x : Result<Unit, string> =
    match x with
    | 1 -> Ok ()
    | _ -> Error "not 1"

Given that Ok is a parameterized type, I don't think there's a way to get Ok by itself to work for what you're doing.

Upvotes: 7

Related Questions