Seq.readonly<'T> Function (F#)
Collapse the table of content
Expand the table of content

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)

// Signature:
Seq.readonly : seq<'T> -> seq<'T>

// Usage:
Seq.readonly source


Type: seq<'T>

The input sequence.

Exception Condition




Thrown when the input sequence is null.

The result sequence.

This function is named ReadOnly in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

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] <- 0
// The following line does not produce an error. 
// It does not preserve encapsulation.
rangeArray.[0] <- 0

Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

© 2015 Microsoft