What does #
mean in type signatures like seq<#seq<'a>>
compared to just seq<seq<'a>>
?
This is called flexible type. The short summary is that #type
means any type that is inherited from type
. So, in your concrete example seq<#seq<'a>>
will be a sequence of any collections containing 'a
values.
When calling a function, F# automatically casts concrete types to interfaces - so for example, you can call a function taking seq<'a>
with array 'a[]
as an argument. However, this does not work when you have array of arrays - because 'a[][]
only implements seq<'a[]>
but not seq<seq<'a>>
.
For example, the following two functions return list of lengths of nested sequences:
let f1 (s:seq<seq<'T>>) = [ for i in s -> Seq.length i ]
let f2 (s:seq<#seq<'T>>) = [ for i in s -> Seq.length i ]
But only the second one can be called on lists of lists:
[ [1]; [2;3] ] |> f1
// error FS0001: The type 'int list list' is not
// compatible with the type 'seq<seq<'a>>'
[ [1]; [2;3] ] |> f2
// val it : int list = [1; 2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With