5

Say, I have something like this. It does not compile, but you can see what I'm trying to do. I've tried to google every which way, but no dice. Can this be done?

let inline read (s:string) : ^x =
    let parsed = (^x : (static member ofString: string -> ^x option) (s))

    // this is the bit I'm not sure how do to. This doesn't compile.
    // I'm trying to determine what the statically chosen type for ^x is.
    let t = typeof<^x>

    match parsed with
    | Some y -> y
    | None -> failwithf "can't parse %s into %s" s (t.Name)

1 Answer 1

10

This works fine and you can use typeof on statically resolved parameters - the issue is that the parser cannot deal with <^, because it can also be parsed as an operator.

You can fix this easily just by adding spaces around ^x:

let t = typeof< ^x >
1
  • 3
    Aw man, this is depressing! Took me long enough to research, and then stackoverflow, just to find out I needed a blank space!
    – Daniel
    Jul 14, 2017 at 20:37

Your Answer

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

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