Table of contents
TOC
Collapse the table of content
Expand the table of content

Seq.readonly<'T> Function (F#)

Den Delimarsky|Last Updated: 8/18/2017
|
1 Contributor

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)

Syntax

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

// Usage:
Seq.readonly source

Parameters

source Type: seq<'T>

The input sequence.

Exceptions

ExceptionCondition
ArgumentNullExceptionThrown when the input sequence is null.

Return Value

The result sequence.

Remarks

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.

Example

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

Platforms

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

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Collections.Seq Module (F#)

Microsoft.FSharp.Collections Namespace (F#)

© 2017 Microsoft