Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallowed changing the items within NonEmptyEnumerable. #157

Merged
merged 1 commit into from
Oct 12, 2023
Merged
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
20 changes: 16 additions & 4 deletions src/FuncSharp/Collections/NonEmptyEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,23 @@ private NonEmptyEnumerable(T head, IReadOnlyList<T> tail)

public int Count { get; }

public T this[int index] => index switch
public T this[int index]
{
0 => Head,
_ => Tail[index - 1]
};
get
{
if (index < 0 || index >= Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}

return index switch
{
0 => Head,
_ => Tail[index - 1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this already throw the argument exception? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I suppose it does, but this is what I've found in the dotnet runtime repository in Grouping. If it's good enough for them, it's good enough for me 😄

};
}
set => throw new NotSupportedException("Cannot modify items within NonEmptyEnumerable.");
}

public IEnumerator<T> GetEnumerator()
{
Expand Down