3

I need to call bool.TryParse in F# and I'm having a little difficulty with the compiler / Ionide.

Ex.

let value = true
let parsable = bool.TryParse("True", &value)

There error I'm seeing in Ionide for VSCode looks like the following.

val value : bool Full name: value

Assembly: example3

The type 'ByRefKinds.InOut' does not match the type 'ByRefKinds.In'F# Compiler(1)

This is the first example that I've used call by ref keywords and syntax in F# so I'm not sure what I'm doing wrong and the byrefs documentation doesn't seem to be of much help on understanding this particular scenario.

2 Answers 2

7

It turns out that the main thing I was missing in my example was that value wasn't mutable as it would need to be when called in this context.

Working example:

let mutable value = true
let parsable = bool.TryParse("True", &value)

This solution came to me after reading a slightly related GitHub issue related to Span<T> and then also playing around in dotnet fsi which at least gave me the following clue.

  let parsable = bool.TryParse("True", &value);;
  -------------------------------------^^^^^^

stdin(34,38): error FS3230: A value defined in a module must be mutable in order to take its address, e.g. 'let mutable x = ...'

As it turns out however F# also seems to have sntaxtic sugar around the Try-Parse pattern which I began to recall after playing around with this example further. This reduced into the below alternative solution to calling bool.TryParse.

let value = true
let (parsable, _) = bool.TryParse "True"

Alternatively without having to bind parsable and to specify the default value within a single expression the following example may be more elegant.

let value = match bool.TryParse "True" with
| true, value -> value
| false, _ -> true

Perhaps there are pros and cons to the different ways of calling bool.TryParse and other Try-Parse methods but the important thing is that I found some solutions that work and get me past the initial stumbling blocks with regard to lack of F# documentation on the subject.

1
  • 1
    Your last example seems the best to me. If you didn't want a default value, then the false case could raise an exception or retry or do something else. Commented Dec 9, 2019 at 16:06
1

As you have already discovered, the error can be fixed by making the 'value' a mutable variable. Saying that, the last solution solution you shared (using pattern matching) looks the nicest and safest.

If you happen to do many of these parsing operations, I would suggest extracting some utility functions. That will avoid having to correctly pattern match on (true, value) rather than (false, value) by mistake.

Sample code:

open System

module Option =
    let ofTry<'a> (res:(bool*'a)) : 'a option =
        match res with
        | true, res -> Some res
        | false, _ -> None

module Result =    
    let ofTry<'a,'b> (err:'b) (res:(bool*'a)) : Result<'a, 'b> =
        res
        |> csharpToOption
        |> Option.toResult err

module Samples =
    let withPatternMatch =
        match Int32.TryParse "5" with
        | true, value -> value |> Some
        | false, _ -> None

    let withOptionHelper = Int32.TryParse "5" |> Option.ofTry

    let withResultHelper = Int32.TryParse "5" |> Result.ofTry "Not a number"
2
  • Mabye tryParseTupleToOption?
    – jpierson
    Commented Dec 11, 2019 at 17:27
  • I quite like Option.ofTry & Result.ofTry now - it's also nice to put them in the relevant type's module
    – JSparrow
    Commented Mar 27, 2020 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.