type T() =
static member (~%)(t : T) = t
let t = T()
let t' = %t // FAILS
The error message says t was expected to be of type Quotation.Expr<'a>.
% is a supposedly valid prefix operator, but is it possible to actually use it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The reason why you are seeing this behavior is because F# does not define
(~%)with static constraints like most top-level operators. It is defined as a functionQuotations.Expr<'a> -> 'a. Hence, the(~%)function (which is an alias forop_Splice) you defined on typeTis not resolved by uses of the top-level(~%)operator.You can see this by the following FSI interaction:
Thus if we redefine the top-level
(~%)operator as follows, then your example will compile without error:but do note that quotation splicing will no longer work:
that’s because the original definition of
(~%)is treated specially by the compiler for quotation splicing. Indeed, you can see in theExprandExpr<'T>signatures that those types do not define any operators at all, let aloneop_Splice.You can see similar results with
&&and||infix operators. Which can be redefined (mapping toop_BooleanAndandop_BooleanOr), but unless they are, they are treated specially by the compiler.