gpt4 book ai didi

f# - 在 seq builder 中枚举时,F# 如何知道 bitArray 元素是 bool ?

转载 作者:行者123 更新时间:2023-12-03 23:44:57 28 4
gpt4 key购买 nike

    seq{
for bit in BitArray(10) do
yield bit
}
bitbool类型。我检查了 ILSpy 并且在生成的一个闭包中添加了一个显式转换。
BitArray仅实现普通(非通用) IEnumerable . F# 怎么知道它是 bool ?

最佳答案

According to the F# 4.1 specification's Section 6.5.6 Sequence Iteration Expressions , F# 甚至对非泛型 IEnumerable 也进行强制转换如果IEnumerable有一个 Itemobject 的属性(property)类型(突出显示我的):

An expression of the following form is a sequence iteration expression:

for pat in expr1 do expr2 done

The type of pat is the same as the return type of the Current property on the enumerator value. However, if the Current property has return type obj and the collection type ty has an Item property with a more specific (non-object) return type ty2, type ty2 is used instead, and a dynamic cast is inserted to convert v.Current to ty2.



If we look at the source code for BitArray ,我们看到它确实有一个 Item类型为 bool 的属性:
public bool this[int index] {
get {
return Get(index);
}
set {
Set(index,value);
}
}

因此,F# 将显式转换为 bool在迭代时。

关于f# - 在 seq builder 中枚举时,F# 如何知道 bitArray 元素是 bool ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54188469/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com