Skip to main content

Command Palette

Search for a command to run...

The Accessor Protocol

An accessor protocol for generalized array-like object iteration.

Updated
41 min readView as Markdown
The Accessor Protocol
A

Athan is a software engineer at Quansight and core developer of stdlib.

In this post, I'll introduce you to the accessor protocol for generalized array-like object iteration. First, I'll provide an overview of built-in array-like objects, along with example usage. I'll then show you how you can create your own custom array-like objects using the same element access syntax. Next, we will explore why you might want to go beyond vanilla array-like objects to create more "exotic" variants to accommodate sparsity, deferred computation, and performance considerations. Following this, I'll introduce the accessor protocol and how it compares to possible alternatives. Finally, I'll showcase various example applications.

Sound good?! Great! Let's go!

TL;DR

The accessor protocol (also known as the get/set protocol) defines a standardized way for non-indexed collections to access element values. In order to be accessor protocol-compliant, an array-like object must implement two methods having the following signatures:

function get<T