I have the following method:
member this.addColumnWithHeading heading column =
this.addColumn (seq { yield heading; yield! (column |> Seq.map string)})
which takes a string heading and any sequence (which is compiled to seq in this case), creates a sequence of strings and calls another method with this data. However, it doesn’t work with column being a sequence of floats:
Error 1 The type 'obj' does not match the type 'float' C:\Users\ga1009\Documents\PhD\cpp\pmi\fsharp\pmi\Program.fs 138
How can I define the method addColumnWithHeading so that it works with floats as well?
The built-in
stringfunction is an inline function which uses a statically-resolved generic parameter; since youraddColumnWithHeadingmethod is not declaredinline, the F# type inference has to assume the values in the sequence are of typeobj.There’s a simple solution though — swap out the
stringfunction in favor of “manually” calling.ToString()on the values in the sequence. If you do that, F# will be able to use a standard generic parameter type for the sequence so you can pass a sequence of any type you desire.