Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,20 @@ type SeqModule2() =
for i = 11 to 20 do
CheckThrowsArgumentException (fun () -> Seq.item i { 10 .. 20 } |> ignore)

[<Test>]
member this.``item should fail with correct number of missing elements``() =
try
Seq.item 0 (Array.zeroCreate<int> 0) |> ignore
failwith "error expected"
with
| exn when exn.Message.Contains("seq was short by 1 element") -> ()

try
Seq.item 2 (Array.zeroCreate<int> 0) |> ignore
failwith "error expected"
with
| exn when exn.Message.Contains("seq was short by 3 elements") -> ()

[<Test>]
member this.Of_Array() =
// integer Seq
Expand Down
3 changes: 2 additions & 1 deletion src/fsharp/FSharp.Core/seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ namespace Microsoft.FSharp.Collections

let rec nth index (e : IEnumerator<'T>) =
if not (e.MoveNext()) then
let shortBy = index + 1
invalidArgFmt "index"
"{0}\nseq was short by {1} {2}"
[|SR.GetString SR.notEnoughElements; index; (if index=1 then "element" else "elements")|]
[|SR.GetString SR.notEnoughElements; shortBy; (if shortBy = 1 then "element" else "elements")|]
if index = 0 then e.Current
else nth (index-1) e

Expand Down