F#:如何使用Argument Byref Int调用函数 [英] F#: How to Call a function with Argument Byref Int
本文介绍了F#:如何使用Argument Byref Int调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这段代码:
let sumfunc(n: int byref) =
let mutable s = 0
while n >= 1 do
s <- n + (n-1)
n <- n-1
printfn "%i" s
sumfunc 6
我收到错误:
(8,10): error FS0001: This expression was expected to have type
'byref<int>'
but here has type
'int'
所以从中可以看出问题是什么,但我只是不知道如何解决这个问题。我想我需要以某种方式将数字6指定为 byref< int>
。我只是不知道如何。我的主要目标是使 n
或函数参数可变,以便我可以更改并在函数内使用它的值。
So from that I can tell what the problem is but I just dont know how to solve it. I guess I need to specify the number 6 to be a byref<int>
somehow. I just dont know how. My main goal here is to make n
or the function argument mutable so I can change and use its value inside the function.
推荐答案
这是一个糟糕的主意:
let mutable n = 7
let sumfunc2 (n: int byref) =
let mutable s = 0
while n >= 1 do
s <- n + (n - 1)
n <- n-1
printfn "%i" s
sumfunc2 (&n)
完全赞同munn的评论,这是内爆的另一种方式:
Totally agree with munn's comments, here's another way to implode:
let sumfunc3 (n: int) =
let mutable s = n
while s >= 1 do
let n = s + (s - 1)
s <- (s-1)
printfn "%i" n
sumfunc3 7
这篇关于F#:如何使用Argument Byref Int调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文