Expand Minimize
This topic has not yet been rated - Rate this topic

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
source

Type: seq<'T>

The input sequence.

Exception

Condition

ArgumentNullException

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.