Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 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 Answer

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...