gpt4 book ai didi

f# - 从 F# 中的引用数组访问元素

转载 作者:行者123 更新时间:2023-12-01 05:14:21 26 4
gpt4 key购买 nike

正在尝试使用数组和引用 - 我的目的是创建对数组的引用,然后在闭包内使用该引用来访问数组的特定元素。 FSI 尝试:

> let dk2 = Array.create 5 0
let dk2ref = ref dk2;;

val dk2 : int [] = [|0; 0; 0; 0; 0|]
val dk2ref : int [] ref = {contents = [|0; 0; 0; 0; 0|];}

> !dk2ref.[1]

stdin(3,2): error FS0039: The field, constructor or member 'Item' is not defined

是否有直接的方法来访问引用数组的元素? (在这种情况下,dk2ref 的第二个元素)?

最佳答案

我不确定您为什么需要引用数组。

查找 operator precedence table , . 运算符的优先级高于 ! 运算符。因此,您的示例被解析为 !(dk2ref.[1]),这会导致错误,因为 'a ref 未实现 indexed properties .

你只需要在正确的地方加上括号:

> (!dk2ref).[1]
val it : int = 0

关于f# - 从 F# 中的引用数组访问元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16212596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com