Seq.readonly<'T> Function (F#)
Creates a new sequence object that delegates to the given sequence object. This ensures the original sequence cannot be rediscovered and mutated by a type cast. For example, if given an array the returned sequence will return the elements of the array, but you cannot cast the returned sequence object to an array.
Namespace/Module Path: Microsoft.FSharp.Collections.Seq
Assembly: FSharp.Core (in FSharp.Core.dll)
The following code uses Seq.readonly to create an immutable view of a mutable array.
type ArrayContainer(start, finish) = let internalArray = [| start .. finish |] member this.RangeSeq = Seq.readonly internalArray member this.RangeArray = internalArray let newArray = new ArrayContainer(1, 10) let rangeSeq = newArray.RangeSeq let rangeArray = newArray.RangeArray // These lines produce an error: //let myArray = rangeSeq :> int array //myArray. <- 0 // The following line does not produce an error. // It does not preserve encapsulation. rangeArray. <- 0