Reputation: 890
Was experimenting with arrays and references - my intent is to create a reference to an array and then use that reference inside a closure to access a certain element of the array. FSI attempt:
> 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
Is there a direct way to access the element of a referenced array? (In this case, the 2nd element of dk2ref)?
Upvotes: 3
Views: 603
Reputation: 41290
I'm not sure why you need a reference array.
Looking up the operator precedence table, .
operator has higher precedence than !
operator. So your example is parsed as !(dk2ref.[1])
which causes the error because 'a ref
doesn't implement indexed properties.
You just need to add parentheses at the right places:
> (!dk2ref).[1]
val it : int = 0
Upvotes: 6