Why Kotlin has Mutable Collections

Why Kotlin has Mutable Collections

Mutable and immutable collections individually expose useful features that can’t co-exist in a single interface. Mutable collections are mutable, while immutable collections are covariant.

Everything in Kotlin is slick and elegant— you never have to write unnecessary code, and the primitive language constructs just work. But the dichotomy between mutable collections and immutable collections feels like something that can be avoided. The fact that it’s such an integral part of the language must point to something deeper.

The simple, one-line explanation is this:

Mutable and immutable collections individually expose useful features that can’t co-exist in a single interface. Mutable collections are mutable, while immutable collections are covariant.

If that doesn’t make sense to you, don’t worry. The rest of this article was written to explain it to you!

Type Hierarchy

We need to establish some important things about the type of hierarchy before moving on.

Assign-ability

Consider the following variable:

Image for post

Although the variable is declared with type Shape, you can actually store an object of type Rectangle in it. That’s because Rectangle is a subtype of Shape, so it will have at least all of the properties/fields/methods of Shape implemented.

Therefore, this is valid:

Image for post

Which brings us to the first important point you need to remember:

When assigning an object to a variable, the variable’s type must be a supertype of (or the same as) the type of the incoming object.

It is important to emphasize the distinction between the type of the variable and the type of the object. The variable myShape ‘looks’ like a Shape, but actually holds a Rectangle.

In this sense, the type of the variable defines an interface. When you interact with myShape, you interact with the Rectangle, but you only do so in Shape-specific ways.

programming-languages kotlin-collection kotlin design-patterns object-oriented

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Find Ulimit For user on Linux

Explains how to find ulimit values of currently running process or given user account under Linux using the 'ulimit -a' builtin command.

MEAN Stack Tutorial MongoDB ExpressJS AngularJS NodeJS

MEAN Stack Tutorial MongoDB ExpressJS AngularJS NodeJS - We are going to build a full stack Todo App using the MEAN (MongoDB, ExpressJS, AngularJS and NodeJS). This is the last part of three-post series tutorial.

Creating RESTful APIs with NodeJS and MongoDB Tutorial

Creating RESTful APIs with NodeJS and MongoDB Tutorial - Welcome to this tutorial about RESTful API using Node.js (Express.js) and MongoDB (mongoose)! We are going to learn how to install and use each component individually and then proceed to create a RESTful API.

systemctl List All Failed Units/Services on Linux

Explains how to use the systemctl command to list all failed units or services on Debian, Ubuntu, CentOS, Arch, Fedora, and other Linux distros.

CentOS Linux 8.2 Released and Here is How to Upgrade it

CentOS Linux 8.2 (2004) released. This release adds corrections for security issues based upon RHEL 8.2 souce and here is how to upgrade it.