IO: Describing Side Effects

Describe a side effect now, and run it once, at the edge.

"Heard melodies are sweet, but those unheard / Are sweeter"

— John Keats, Ode on a Grecian Urn

An IO is Keats's unheard melody: until you run it, it is a description you can still arrange and combine, and nothing has played.

IO at a glance

ModelsA side effect described now and run later
ReplacesA method that performs its side effect as soon as it is called
Effect PathIOPath
InstancesFunctor, Applicative, Selective, Monad
Choose it whenThe effect waits until you run it, throws no checked exception, and need not run at the same time as other work; otherwise VTask, or compare every type in Choosing a Type

What You'll Learn

  • Read the time through TimeSource, so code that uses it can be tested with a fixed clock
  • Build a program from steps that each describe an effect, and say when each one runs
  • Say why running an IO twice runs its effects twice
  • Catch a failing step on an IOPath, since IO itself lets the exception through
  • Use IO in generic code through its Monad instance

See Example Code

The code on this page is IOBook.java: the page includes it, so the build compiles and runs it. IOBookTest.java holds what the page says each method does.


Reading the clock

A method that calls Instant.now() reads the clock the moment it is called, so no test can predict its result. Injecting a java.time.Clock fixes the test, but the read still happens at the call. An IO<A> is close to a Supplier<A> that nothing has called yet. What it adds is map and flatMap, which join steps into one description without running any of them, so you build the program in one place and run it in another. TimeSource wraps a Clock: its now() is IO.delay(clock::instant), and nowAsync() gives the same read as a VTask.

The code on this page uses these imports, with Order, Consignment, ConsignmentState and Address from the order records the Optics chapter introduces. Import the library's IO by name: Java 25 has a java.lang.IO too.

import java.util.ArrayList;
import java.util.List;
import org.higherkindedj.example.book.optics.cast.Address;
import org.higherkindedj.example.book.optics.cast.Consignment;
import org.higherkindedj.example.book.optics.cast.ConsignmentState;
import org.higherkindedj.example.book.optics.cast.Order;
import org.higherkindedj.hkt.io.IO;
import org.higherkindedj.hkt.time.TimeSource;

Dispatching an order stamps its consignment with the time:

  // The time is read when the IO runs, not when dispatch is called
  static IO<Consignment> dispatch(TimeSource time, Order order, Address to) {
    return time.now()
        .map(now -> new Consignment(order.id(), to, new ConsignmentState.Dispatched(now)));
  }

NINE_AM is the instant a fixed time source reads, ORDER is a paid order ready to ship, and LEEDS is the address it goes to:

    TimeSource time = TimeSource.fixed(NINE_AM);
    IO<Consignment> described = dispatch(time, ORDER, LEEDS);

    Consignment dispatched = described.unsafeRunSync();
    // Consignment[orderId=00000000-0000-0000-0000-000000000001,
    //   to=Address[street=1 High Street, city=Leeds, postcode=LS1 4AP],
    //   state=Dispatched[at=2026-10-31T09:00:00Z]]

Nothing is read until the effect runs, and each run reads afresh. In tests, inject TimeSource.fixed(...), or TimeSource.of(steppableClock) with hkj-test's SteppableClock, and the same code becomes deterministic. It is named TimeSource, not Clock, so that it never clashes with java.time.Clock.

Describing, then running

flatMap joins a step that needs the result of the one before, and map changes the result. Both build a new IO, and neither runs anything. IO.delay describes any effect, here adding the consignment to a log:

  static IO<Consignment> record(List<Consignment> log, Consignment consignment) {
    return IO.delay(
        () -> {
          log.add(consignment);
          return consignment;
        });
  }

  static IO<Consignment> dispatchAndRecord(
      TimeSource time, List<Consignment> log, Order order, Address to) {
    return dispatch(time, order, to).flatMap(consignment -> record(log, consignment));
  }

unsafeRunSync runs the whole description, in the order it was built. Its name warns that this is where the effects happen: call it once, at the edge of the application, never deep inside its logic:

    List<Consignment> log = new ArrayList<>();
    IO<Consignment> program = dispatchAndRecord(time, log, ORDER, LEEDS);

    int beforeRunning = log.size();
    // 0
    program.unsafeRunSync();
    program.unsafeRunSync();
    int afterTwoRuns = log.size();
    // 2

The log is empty until the program runs, and holds two consignments after two runs. An IO is a description, not a stored result: each run does the work again, and reads the clock again. To reuse a result, run the IO once and keep its value. Lazy keeps the first result of a computation with no side effect.

On an IOPath

IO has no MonadError instance: an exception a step throws leaves unsafeRunSync, as it would leave a direct call. Application code usually holds its effects on an IOPath, which adds the failure handling: catching turns an exception into a Left, handleError into a value, and asTry into a Try. These are the extra imports:

import org.higherkindedj.hkt.effect.IOPath;
import org.higherkindedj.hkt.effect.Path;
import org.higherkindedj.hkt.either.Either;

bookCourier stands in for a courier call that throws. Path.ioPath puts an IO on a Path, via chains the next step, and catching turns the exception into a Left. An IOPath runs with unsafeRun, as an IO runs with unsafeRunSync:

  static IO<Consignment> bookCourier(Consignment consignment) {
    return IO.delay(
        () -> {
          throw new IllegalStateException("the courier is closed");
        });
  }

  static IOPath<Either<String, Consignment>> dispatchSafely(
      TimeSource time, Order order, Address to) {
    return Path.ioPath(dispatch(time, order, to))
        .via(consignment -> Path.ioPath(bookCourier(consignment)))
        .catching(error -> "booking failed: " + error.getMessage());
  }

    Either<String, Consignment> refused = dispatchSafely(time, ORDER, LEEDS).unsafeRun();
    // Left(booking failed: the courier is closed)

IOPath also releases resources with bracket and withResource, and retries or times out a step, as IOPath shows.

Creating instances

IO.delay(supplier) describes an effect that gives a value, and IO.fromRunnable(action) one that gives Unit. A plain value goes in as IO.delay(() -> value), or as Path.ioPure(value) on a Path. IO.delay takes a Supplier, which cannot throw a checked exception, so catch one inside the lambda, as IOPath shows, or use VTask, whose Callable can throw one. An IO always gives a value: a supplier or map function that returns null fails the IO when it runs, and the instance's of(null) throws at the call, as Values, never null sets out. NullAway flags a lambda or method reference given to IO.delay that returns null only with the options in What a nullness checker sees. It flags a Supplier<@Nullable T> variable given to IO.delay in JSpecify mode alone.

Methods

MethodWhat it does
IO.delay(supplier)Describes an effect; the supplier runs only when the IO does
IO.fromRunnable(action)Describes an effect that gives Unit
map(f)Builds an IO whose result is changed by f, running nothing
flatMap(f)Builds an IO that runs the next step on this one's result, running nothing
asUnit()Keeps the effect and replaces the result with Unit
unsafeRunSync()Runs the description, blocking the calling thread until it gives its result or throws its exception
TimeSource.system(), fixed(instant), of(clock)Give a TimeSource over the system clock in UTC, a fixed instant, or any Clock
timeSource.now(), nowAsync()Give an IO<Instant>, or a VTask<Instant>, that reads the time when it runs

Working with Kind

Generic code, written once for any container, takes IO through its Monad instance, from Instances.monad(io()). The instance works over Kind<IOKind.Witness, A>, so you widen an IO to pass it in, and run the result with IO_OP.unsafeRunSync. These are the extra imports:

import static org.higherkindedj.hkt.instances.Witnesses.io;
import static org.higherkindedj.hkt.io.IOKindHelper.IO_OP;

import org.higherkindedj.hkt.Kind;
import org.higherkindedj.hkt.Monad;
import org.higherkindedj.hkt.instances.Instances;
import org.higherkindedj.hkt.io.IOKind;
    Monad<IOKind.Witness> effects = Instances.monad(io());

    Kind<IOKind.Witness, Consignment> stamped = IO_OP.widen(dispatch(time, ORDER, LEEDS));
    Kind<IOKind.Witness, String> label =
        effects.map(consignment -> "to " + consignment.to().city(), stamped);

    String ran = IO_OP.unsafeRunSync(label);

ran is "to Leeds". The instance's map builds a new IO, and nothing reads the clock until IO_OP.unsafeRunSync runs it. The Monad helpers, such as peek and flatMapIfOrElse, work on it too, as Helpers built on flatMap and map shows. Widening allocates nothing: IO implements IOKind itself, so widen checks for null and returns the same object. IO has a Selective instance too, an object of its own, and IO at a glance, at the head of the page, lists it.

When to use IO

You needUse
A side effect described now and run once, at the edgeIO
The same, with failure handling, resources and retriesIOPath
A value worked out once and kept, with no side effectLazy
An effect that throws a checked exception, or runs at the same time as other workVTask
Work that has already started, on another threadCompletableFuture
A program as data, which several interpreters can runFree

Hands-On Learning

Practise deferring a side effect with IOPath, in Exercise 3 of Tutorial 01: Effect Path Basics (9 exercises).

Benchmarks: IOBenchmark measures building and running IO chains, and VTaskVsIOBenchmark compares them with VTask. Benchmarks & Performance says how to run them.


Previous: Optional Next: Lazy