I am wanting to format a tuple in a specific way and I am trying to do that by checking the type of the tuple (2 element, 3 element etc.). I am getting an error on the third line saying:
This runtime coercion of type test from type
'd
to
'a * ('b * 'c)
involves an indeterminate type based on the information prior to this program point.
Runtime type tests are not allowed on some type. Further type annotations are needed.
Here is my attempt:
let namer x =
match x with
| :? ('a * ('b * 'c)) as a, b, c -> sprintf "%s_%s_%s" (a.ToString()) (b.ToString()) (c.ToString())
| :? ('a * 'b) as a, b -> sprintf "%s_%s" (a.ToString()) (b.ToString())
| a -> sprintf "%s" (a.ToString())
How should you do something like this? I want to be able to format the string based on the type of tuple.
What I am ultimately wanting is to be able to "flatten" a nested tuple to a string without a bunch of parenthesis. For example:
// What I want
let x = (1, (2, (3, 4)))
let name = namer x
printfn "%s" name
> 1_2_3_4
Update: This is different than the question "How can i convert between F# List and F# Tuple?" found here. I know how to do that. What I am wanting is to be able to detect if I have a tuple and what type of tuple. The ideal is a generic function that could take a single element, a tuple, or nested 2 element tuples. For example, legal arguments would be:
let name = namer 1
// or
let name = namer (1, 2)
// or
let name = namer (1, (2, 3))
// or
let name = namer (1, (2, (3, 4)))
I also want to handle non-integer values. For example:
let name = namer (1, ("2", (3, "chicken")))