小编Jim*_*mmy的帖子

F#Quotations - 遍历由Value表示的函数调用

我花了几个小时试图掌握F#Quotations,但我遇到了一些路障.我的要求是从一个有区别的联合类型中取出简单的函数(只是整数,+, - ,/,*)并生成一个最终将用于生成C代码的表达式树.我知道这可以使用带有"直接"功能的报价.

我的问题是表达式树似乎以"值"终止,我无法弄清楚如何遍历该值.

我的问题是在这种情况下这是否真的可行?还是有其他值得考虑的方法.

type FuncType = 
| A of (int -> int -> int)
| B
| C

[<ReflectedDefinition>]
let add x y = x + y


let myFunc1 = A (fun x y -> x + y )
let myFunc2 = A add 

let thefunc expr = 
    match expr with
    | A(x) ->
        <@ x @>
    | _ ->
        failwith "fail"

printfn "%A" (thefunc myFunc1) // prints "Value (<fun:myFunc1@14>)"
printfn "%A" (thefunc myFunc2) // prints "Value (<fun:myFunc2@15>)" …
Run Code Online (Sandbox Code Playgroud)

f#

4
推荐指数
1
解决办法
494
查看次数

F# - 将参考单元传递给函数

我只是想知道是否有人可以向我解释如何将参考单元传递给非类成员的函数.我一直在关注msdn页面msdn参考单元格

我有以下代码:

let myint = ref 32
let mutable myint2 = 23

type addone() = 
    member t.myadd1func (x:int byref) = 
        x <- x + 1

let myadd1func (x:int byref) = 
    x <- x + 1

let adder = new addone()

adder.myadd1func myint
// myadd1func myint <---- this line does not compile

myadd1func &myint2 // <----- this line does though


printfn "%d" !myint
printfn "%d" myint2
Run Code Online (Sandbox Code Playgroud)

我的问题是......我对该类的"Myadd1func"方法和之后定义的"myadd1func"函数的调用之间的根本区别是什么?

在我写这篇文章时,我猜测该函数不喜欢将.net对象引用传递给它,因为这可能会破坏与其他IL组件的兼容性?我不介意使用可变值,我只是想了解这些事情.

谢谢

f#

2
推荐指数
1
解决办法
723
查看次数

标签 统计

f# ×2