liveBook · Manning

Return

As explained in Functional Programming in Scala, Second Edition

Return is an IO data constructor representing a computation that has completed with a value and requires no further steps.

Return is an IO data constructor for a computation that has finished. It holds a value a and signals that interpretation can produce that value without performing further steps. section 13.3.1

Distinguish Return from the other computation constructors:

  • Return(a) represents a completed computation whose result is a. section 13.3.1
  • Suspend represents an effect that must be executed to produce a result. section 13.3.1
  • FlatMap continues an existing computation by applying a function to its result and producing the next computation. section 13.3.1

Return inside map

map can be expressed through flatMap by wrapping the transformed value in Return: flatMap(a => Return(f(a))). During interpretation, a Return encountered beneath FlatMap lets the interpreter apply the continuation immediately. section 13.10

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines Return and traces its interpreter behavior, including simplification beneath FlatMap, but defers broader runtime concerns.
Examples
Uses map, printLine, repeated IO structure, and Free interpreters to illustrate Return construction and evaluation.
What it assumes
Assumes familiarity with Scala data constructors and flatMap while establishing IO control flow through explicit cases.
What you can do after
Evaluate/understand how completed values participate in composable IO programs and interpreter pattern matches.
How it connects
Connects Return to IO, Suspend, FlatMap, map, interpreters, tail recursion, and free monads.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.