3

Attempting to write more queries in F# through LinqPad I ran into an interesting thing with the where clause.

let dc = new TypedDataContext()

let q = query {
    for o in dc.OrderItem do 
    where (o.Description.Contains("spam"))
    select o
}

q |> Dump

If I remove the parenthesis around o.Description.Contains("spam") I get the often seen error message below.

Successive arguments should be separated by spaces or tupled, and arguments involving function or method applications should be parenthesized (using external F# compiler)

When I see this error I normally realize that I'm not giving enough information in order for the compiler to understand that I'm attempting to chain off of the result of a prior method call or property get accessor invocation but in this case it is unclear to me. Additionally I'd be curious if there is a more idiomatic way of satisfying the conditions of the compiler without having to reach around the expression to add both open and closing parenthesis.

1 Answer 1

7

You'd see the same result if you did something like this:

let f x = 1

f o.Description.Contains("spam")

As the message implies, if you're using the result of a method call (Contains in this case) as an argument to a function, then the method call needs to be parenthesized. Even though where is a query operator rather than a true function, the same result applies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.