Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...