Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does F# know that bitArray elements are bool while enumerating in seq builder?

Tags:

f#

    seq{
        for bit in BitArray(10) do 
        yield bit
    }

bit is of bool type. I checked with ILSpy and there's an explicit cast added in one of closures generated.

BitArray implements only plain (not generic) IEnumerable. How does F# know that it's a bool?

like image 707
Artur Tadrała Avatar asked Jan 14 '19 20:01

Artur Tadrała


People also ask

What is the formula of f x?

The slope of a linear function is calculated by rearranging the equation to its general form, f(x) = mx + c; where m is the slope. The vertex of a quadratic function is calculated by rearranging the equation to its general form, f(x) = a(x – h)2 + k; where (h, k) is the vertex.

What does f mean in math?

more ... A special relationship where each input has a single output. It is often written as "f(x)" where x is the input value. Example: f(x) = x/2 ("f of x equals x divided by 2")

What does f say about f?

What Does f ' Say About f ? The first derivative of a function is an expression which tells us the slope of a tangent line to the curve at any instant. Because of this definition, the first derivative of a function tells us much about the function.


1 Answers

According to the F# 4.1 specification's Section 6.5.6 Sequence Iteration Expressions, F# does casting even for a non-generic IEnumerable if the IEnumerable has an Item property with a non-object type (highlighting mine):

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, we see that it does indeed have an Item property with type bool:

public bool this[int index] {
        get {
            return Get(index);
        }
        set {
            Set(index,value);
        }
}

Thus, F# will explicitly cast to bool while iterating.

like image 162
Ringil Avatar answered Oct 16 '22 19:10

Ringil