f# - fsharp assign value by referencing via square brackets -


for fsharp array can following:

let tt = array.zerocreate 10  tt.[5] <- 4 console.writeline("{0}", tt.[5]) 

result 4

i want implement own class provides similar interface. sample, have wrote this:

type mybytearray = class     val data : byte[]      new (size) =         {             data = array.init size (fun x -> byte(x))         }      member this.item (id) =          this.data.[id] end  let test = mybytearray 5 console.writeline("{0}", test.[2]) /// <- 1 woks test.[2] <- 33uy /// <- 1 fails 

this can receive item via [], can't set item.[id] <- newvalue.

how implement such interface?

thank you.

you can define indexer getter , setter this:

member this.item    id = this.data.[id]   , set id v = this.data.[id] <- v 

if writing code, use primary constructor syntax:

type mybytearray(size) =   let data = array.init size (fun x -> byte(x))     member this.item      get(id) = data.[id]     , set id v = data.[id] <- v 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -