#(井号)是什么意思在类型签名? [英] What does # (pound sign) mean in type signatures?

查看:166
本文介绍了#(井号)是什么意思在类型签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#代表 seq<#seq<'a>> 类型签名中的意思是与 seq< seq<'a& > c>

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

推荐答案

这称为灵活类型。简短的总结是 #type 是指继承自类型的任何类型。因此,在你的具体例子中, seq <#seq > 将是包含'a value。

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.

当调用函数时,F#自动将具体类型转换为接口 - 例如,你可以调用 seq< 'a> 与数组'a [] 作为参数。然而,当你有数组数组时,这不工作 - 因为'a [] [] 只实现 seq<'a []& / code>但不是 seq< seq<'a>>

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]

这篇关于#(井号)是什么意思在类型签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆