How to use F # headOrDefault for an empty array?

I want to test headOrDefault in F # on an empty array that should work like FirstOrDefault in C #. So I typed this code:

let arr = []

let res = query {
    for e in arr do
    select e
    headOrDefault
}

Console.WriteLine("{0}", (res = null))

I would expect to resget a null value.

But this code does not even compile. I get this compilation error:

Internal common functions are not allowed in quoted expressions. Consider adding type constraints until this function is no longer common.

The compiler underlines arrin this line as a problem:

for e in arr do

Why doesn't this compile and how can I make it work (using headOrDefault)?

Thank!

+4
source share
2 answers

[] , , , , . , [||] Array.empty.

, , , , .. , /. System.Object, ( System.Object , , -, ):

let emptyarr:System.Object list = []
let res = query {
    for e in emptyarr do
    select e
    headOrDefault
}
System.Console.WriteLine("{0}", (res = null))
+4

let arr = [] : obj list, .

+1

All Articles