开发者

F# quotation evaluation issue

开发者 https://www.devze.com 2022-12-21 04:38 出处:网络
I\'m getting an issue with the F# powerpack quotation evaluation. open Microsoft.FSharp.Linq.QuotationEvaluation

I'm getting an issue with the F# powerpack quotation evaluation.

open Microsoft.FSharp.Linq.QuotationEvaluation

let print x = System.Console.WriteLine(sprintf "%A" x)

type record = { x:int; y:int }
let val1 = { x = 1; y = 1; }
let val2 = { x = 1; y = 1; }
let result = val1 = val2
print result

let quote = <@ let value1 = { x = 1; y = 1; }
               let value2 = { x = 1; y = 1; }
               let result2 = value1 = value2
               result2 @>

print (quote.EvalUntyped())

The开发者_高级运维 first result is true as you would expect. The second is false. Is this a bug, or am I missing something?


This looks like a bug to me. Someone from the F# team will probably give a clear answer on this :-). In the meantime, here is a simple workaround that you can use - The problem seems to be with the compilation of the = operator. You can define your own operator (or a function) and call this operator from the quoted code:

let (><) a b = a = b
let quote = 
 <@ let value1 = { x = 1; y = 1; } 
    let value2 = { x = 1; y = 1; } 
    let result2 = value1 >< value2
    result2 @>      
print (quote.EvalUntyped()) 

Instead of generating a wrong call to the standard = operator, this will generate code that calls your custom operator (which then runs the comparison as a standard, correctly compiled F# code), so this gives the expected result.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号