正在尝试使用数组和引用 - 我的目的是创建对数组的引用,然后在闭包内使用该引用来访问数组的某个元素.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
Run Code Online (Sandbox Code Playgroud)
有没有直接的方法来访问引用数组的元素?(在这种情况下,dk2ref的第二个元素)?
我不确定你为什么需要一个参考数组.
查找运算符优先级表,.运算符的优先级高于!运算符.因此,您的示例将被解析为!(dk2ref.[1])导致错误,因为'a ref未实现索引属性.
您只需要在正确的位置添加括号:
> (!dk2ref).[1]
val it : int = 0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
471 次 |
| 最近记录: |