A read-only environment is shared contextual data that computations can inspect without modifying. In a file-synchronization application, configuration such as source and destination paths, file-type filters, recursion limits, and transfer limits belongs in this environment. section 15.1.1 section 15.1
Use the Reader abstraction to avoid threading configuration through every action:
- Reader r a reads an environment of type r and produces a result of type a. section 15.1.1
- ask retrieves the current environment inside a Reader computation, while runReader supplies the environment from outside. section 15.1.1
- ReaderT adds the same read-only capability to another monad, such as IO, allowing configuration access alongside effectful operations. section 15.1.1
Reading synchronization paths
Define type App a = ReaderT Config IO a. An action can use ask to obtain Config, select configFromPath and configToPath, and use liftIO to print or otherwise perform IO. Running runReaderT example cfg supplies the shared configuration once instead of passing cfg through every action.
