Versatile and composable lenses for Scala
A lens is an object which is able to access and modify specific parts of a
(potentially) complex immutable data structure. It consists of a getter for
access, and a setter for modification. For example, a lens could focus on the
id
field of a User
case class; its getter would take an instance of User
and return its id
, while the setter would take an existing User
instance
and a new id
value, and return a new instance of User
with the new id
,
and all other fields unchanged.
Lenses are notable for their composability. A single lens can focus on a field inside a case class nested inside another case class, or more deeply nested fields. In such an example, a single lens, composed from simpler lenses, could create a new instance of the outermost case class with just one of its innermost field modified, in a single operation, avoiding potentially very complex syntax involving making copies of each intermediate case class.
Panopticon provides concise and elegant syntax for working with lenses.
- create lenses for accessing and modifying deeply-nested fields in case classes
- no performance penalty at runtime over handwritten modification code
- compose lenses with the
++
operator
All terms and types are defined in the panopticon
package, which can be imported with,
import panopticon.*
Imagine we have two case classes,
import anticipation.*
case class User(id: Int, name: Text, birthday: Date)
case class Date(day: Int, month: Int, year: Int)
describing users of a hypothetical system, and numerical calendar dates.
We can construct a Lens
for modifying a user's name with:
val userName = Lens[User](_.name)
Step-by-step, the expression is a reference to the Lens
factory object, the
type upon which the lens will operate (User
), and a lambda from that type to
the field the lens will focus on. The User
type cannot be inferred, but the
type of name
, which is Text
, will be inferred.
Similarly, we could construct a lens which accesses the month field of a Date
with Lens[Date](_.month)
. Or we could construct a lens which directly access
the year of birth of a user, like so:
val userBirthYear = Lens[User](_.birthday.year)
If we construct an instance of a User
with,
val user = User(38295, t"Bob Mason", Date(12, 7, 1997))
then we can always access the user's birth year with user.birthday.year
, but
we can also use the lens created above, userBirthYear
, like so:
val birthYear: Int = userBirthYear.get(user)
This is unspectacular. But it becomes more useful for updating a user.
We can change that user's birth year with,
val user2: User = userBirthYear.set(user, 1995)
which compares favorably with the equivalent code written using the case
classes' copy
methods:
val user2: User = user.copy(birthday = user.birthday.copy(year = 1995))
We can also specify the lens inline in the expression, like so:
val user2: User = Lens[User](_.birthday.year).set(user, 1995)
The saving on syntactic verbosity improves the deeper the nesting.
Although the userBirthYear
lens above was created in a single expression, it
is also possible to construct it by composing a lens for User#birthday
and a
lens for Date#year
, using the ++
operator:
val userBirthday = Lens[User](_.birthday)
val dateYear = Lens[Date](_.year)
val userBirthYear = userBirthday ++ dateYear
Panopticon is classified as embryotic. For reference, Soundness projects are categorized into one of the following five stability levels:
- embryonic: for experimental or demonstrative purposes only, without any guarantees of longevity
- fledgling: of proven utility, seeking contributions, but liable to significant redesigns
- maturescent: major design decisions broady settled, seeking probatory adoption and refinement
- dependable: production-ready, subject to controlled ongoing maintenance and enhancement; tagged as version
1.0.0
or later - adamantine: proven, reliable and production-ready, with no further breaking changes ever anticipated
Projects at any stability level, even embryonic projects, can still be used, as long as caution is taken to avoid a mismatch between the project's stability level and the required stability and maintainability of your own project.
Panopticon is designed to be small. Its entire source code currently consists of 164 lines of code.
Panopticon will ultimately be built by Fury, when it is published. In the meantime, two possibilities are offered, however they are acknowledged to be fragile, inadequately tested, and unsuitable for anything more than experimentation. They are provided only for the necessity of providing some answer to the question, "how can I try Panopticon?".
-
Copy the sources into your own project
Read the
fury
file in the repository root to understand Panopticon's build structure, dependencies and source location; the file format should be short and quite intuitive. Copy the sources into a source directory in your own project, then repeat (recursively) for each of the dependencies.The sources are compiled against the latest nightly release of Scala 3. There should be no problem to compile the project together with all of its dependencies in a single compilation.
-
Build with Wrath
Wrath is a bootstrapping script for building Panopticon and other projects in the absence of a fully-featured build tool. It is designed to read the
fury
file in the project directory, and produce a collection of JAR files which can be added to a classpath, by compiling the project and all of its dependencies, including the Scala compiler itself.Download the latest version of
wrath
, make it executable, and add it to your path, for example by copying it to/usr/local/bin/
.Clone this repository inside an empty directory, so that the build can safely make clones of repositories it depends on as peers of
panopticon
. Runwrath -F
in the repository root. This will download and compile the latest version of Scala, as well as all of Panopticon's dependencies.If the build was successful, the compiled JAR files can be found in the
.wrath/dist
directory.
Contributors to Panopticon are welcome and encouraged. New contributors may like to look for issues marked beginner.
We suggest that all contributors read the Contributing Guide to make the process of contributing to Panopticon easier.
Please do not contact project maintainers privately with questions unless there is a good reason to keep them private. While it can be tempting to repsond to such questions, private answers cannot be shared with a wider audience, and it can result in duplication of effort.
Panopticon was designed and developed by Jon Pretty, and commercial support and training on all aspects of Scala 3 is available from Propensive OÜ.
The Panopticon, meaning "all-seeing", was a concept for the design of prisons, created by Jeremy Bentham, a philosopher and social reformer. Its name is given to this library for the seeing capabilities of lenses.
In general, Soundness project names are always chosen with some rationale, however it is usually frivolous. Each name is chosen for more for its uniqueness and intrigue than its concision or catchiness, and there is no bias towards names with positive or "nice" meanings—since many of the libraries perform some quite unpleasant tasks.
Names should be English words, though many are obscure or archaic, and it should be noted how willingly English adopts foreign words. Names are generally of Greek or Latin origin, and have often arrived in English via a romance language.
The logo shows an optical lens.
Panopticon is copyright © 2024 Jon Pretty & Propensive OÜ, and is made available under the Apache 2.0 License.