-
Notifications
You must be signed in to change notification settings - Fork 56
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
Make all immutable interfaces sealed
to prevent implementations that allow mutations
#147
Comments
I don't think this is necessary. Wrongfully implementing an interface will always cause problems. |
If the implementation of the interface can be mutable, then it is not "immutable" it is just a readonly interface similar to |
Correct. But immutability is a contract. Just like Java's mutability is a contract broken by Guava's immutable collections. Making Consider this: |
The In the future, we might decide to limit the implementation of these interfaces or expose the implementation classes. So that users can be confident that the implementation is indeed immutable. |
I find that As an example, if I bring in 2 third party SDKs (sdk1 and sdk2) I want to be sure that val immutableList: ImmutableList = sdk1.getData()
val processedData: List = sdk2.process(immutableList)
// immutableList is unchanged |
To add to the example above, if public class Sdk2 {
static void process(List<String> list) {
list.add("foo");
}
} Also, I imagine there are potential performance gains that could be had by knowing all possible implementations of |
To further my point, if we make What you're suggesting applies to any interface: if we couple the implementation with the API (by making the interface sealed), we can get better performance and reliability. But SOLID tells us not to. The interface is open for a reason - to allow alternative implementations - for example, to wrap native list implementations - why would I make them falsely implement mutable list and then wrap them with an adapter instead of just implementing immutable list? |
Right now you can roll your own mutable version of
ImmutableList
with something like this:Even well-meaning implementations of the
ImmutableList
interface might accidentally enable mutability.To make
ImmutableList
(andImmutableCollection
, etc) even more immutable, make all of the the interfaces in this librarysealed
interfaces.The text was updated successfully, but these errors were encountered: