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

后端 未结 1 1197
悲哀的现实
悲哀的现实 2020-12-31 02:21

What does # mean in type signatures like seq<#seq<\'a>> compared to just seq> ?

相关标签:
1条回答
  • 2020-12-31 02:47

    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]
    
    0 讨论(0)
提交回复
热议问题