What does # (pound sign) mean in type signatures?

牧云@^-^@ 提交于 2019-11-30 11:17:50

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]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!