Click here to Skip to main content
Click here to Skip to main content

Tagged as

Immutable collections should not implement mutable interfaces

, 17 Sep 2013
Rate this:
Please Sign up or sign in to vote.
I just read the new blog post about .NETs new mutable collections. It mostly looks really nice, but there is a major problem: They implement mutable interfaces like IList which has an Add() method. The blog post states this: Implementing … Continue reading →

I just read the new blog post about .NETs new mutable collections. It mostly looks really nice, but there is a major problem: They implement mutable interfaces like IList<T> which has an Add() method.

The blog post states this:

Implementing the mutable interfaces

Several people raised the issue that our immutable collection types implement the mutable interfaces. For example, this is what the declaration of ImmutableList looks like:

public sealed class ImmutableList<T> :
        IImmutableList<T>,
        IEnumerable,
        IEnumerable<T>,
        IReadOnlyCollection<T>,
        IReadOnlyList<T>,
        ICollection,
        ICollection<T>,
        IList,
        IList<T>
{
   ...
}

It’s worth pointing out that this declaration does not have a correctness issue – you cannot mutate an instance of ImmutableList by casting it to a mutable interface. For example, calling IList.Add would throw NotSupportedException

Now, why is that such a big issue?

shock

Well. It’s not (in a strict sense). I thought so first. When I saw the throw NotSupportedException(); my internal alert system was blinking “LSP violation” in red and the siren was going all crazy.

But then I started to read more carefully about the interfaces in MSDN. The documentation for the Add() method state that NotSupportedException can be thrown if the collection is read only. It’s therefore not a Liskovs Substitution Principle violation.

You could argue that any interface containting an Add() method is incorrectly defined if that Add() method may not be used for all implementations. It would have been better with an interface like IReadOnlyList that exposes an item accessor.

public interface IReadOnlyList<T> : IEnumerable<T>
{
    T this[int index] { get; }
    int Count { get; }
}

public interface IList<T> : IReadOnlyList<T>
{
    T this[int index] { get; set;  }
    void Add(T item);
    void Remove(T item);
}

(I’m not satisfied with the naming as ‘is-a’ relationships also applies to interfaces, but you might understand what I’m going for.)

Ask yourself. If you got a list through a method like IList<User> GetUsers();, would you ever think that you may NOT use the Add/Remove methods? No, you wouldn’t as the defacto standard has been to expose those through the IEnumerable<T> interface. IEnumerable<T> isn't a good choice in all situtations, but it's the best choice that .NET provides for read-only collections.

The implication of using the mutable interfaces for the immutable types is that our code would have to be something like this when we want to get a mutable collection:

public void ProcessUsers(IList<T> users)
{
    if (myList.IsReadOnly)
        throw new InvalidOperationException("We require a proper list, one were the Add method works");

    // [...]
}

As of now, there is no interface that we can use to communicate that we need to work with mutable collections. And no, using concrete types is not an option. Understand? You force us to either start using our own collection interfaces or resort to runtime errors.

The only reason to the decision that I could find is the following:

However, we received the feedback that people had a hard time figuring out how to pass an immutable list to en existing method that took, for example, IList.

Ask yourself WHY they used IList<T> when an immutable object is required. They could have simply used IEnumerable<T>. My best bet is that they required something that they can traverse multiple times or wanted to know the length of the list. As in such the real solution would probably have been to create something like IReadOnlyList<T>.

Please .NET team, do not use the mutable interfaces, even if it’s technically valid to do so.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

jgauffin
Founder Gauffin Interactive AB
Sweden Sweden
Founder of OneTrueError, a .NET service which captures, analyzes and provide possible solutions for exceptions.
 
blog | twitter
Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralMy vote of 2 PinmemberQwertie24-Sep-13 21:25 
GeneralThughts PinprofessionalPIEBALDconsult23-Sep-13 15:09 
QuestionThere is a perfectly reasonable explanation for this. PinmemberQwertie23-Sep-13 12:58 
AnswerRe: There is a perfectly reasonable explanation for this. Pinmemberjgauffin23-Sep-13 19:23 
GeneralRe: There is a perfectly reasonable explanation for this. PinmemberQwertie24-Sep-13 5:15 
GeneralRe: There is a perfectly reasonable explanation for this. Pinmemberjgauffin24-Sep-13 9:47 
GeneralRe: There is a perfectly reasonable explanation for this. [modified] PinmemberQwertie24-Sep-13 14:24 
GeneralRe: There is a perfectly reasonable explanation for this. Pinmemberjgauffin24-Sep-13 18:32 
GeneralMy vote of 5 Pinmemberstefanveliki17-Sep-13 6:49 
GeneralMy vote of 2 PinmemberWilliam E. Kempf17-Sep-13 4:59 
GeneralRe: My vote of 2 Pinmemberjgauffin17-Sep-13 5:05 
GeneralRe: My vote of 2 PinmemberWilliam E. Kempf17-Sep-13 5:27 
GeneralRe: My vote of 2 Pinmemberjgauffin17-Sep-13 6:20 
GeneralRe: My vote of 2 PinmemberWilliam E. Kempf17-Sep-13 6:41 
GeneralRe: My vote of 2 Pinmemberjgauffin17-Sep-13 7:47 
GeneralRe: My vote of 2 PinmemberWilliam E. Kempf17-Sep-13 9:08 
GeneralRe: My vote of 2 Pinmemberjgauffin17-Sep-13 9:12 
GeneralMy vote of 5 PinmemberfireOut17-Sep-13 1:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 17 Sep 2013
Article Copyright 2013 by jgauffin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid