Chapter 4
Platforms, packages and modules
A Roc program runs on a platform, it's made of modules, and it can use packages. Those three words say where Safari Ride's code lives.
A platform is the layer underneath a Roc program that deals with the outside world. For Safari Ride, that's the screen and the keyboard. The Roc code describes each frame, and the platform paints it. In this page, the platform is WebAssembly and a little JavaScript. On a desktop, the same Roc code runs on a native platform called roc-ray.
Each Roc program is an app, and the app's first line of code names its platform. Here's Safari Ride's app for the web, the whole file:
# Safari, as a page: the wasm module a browser runs. main.roc beside it
# is the same app on roc-ray.
app [Model, program] { lib: "../lib/main.roc", pf: platform "../web/platform/main.roc" }
import lib.WasmApp
import SafariApp
Model : SafariApp.Model
program = WasmApp.program(SafariApp.canvas_app)
Lines that start with # are comments. The first line of code names the platform, and gives it the short name pf.
The first line also names a package, lib. A package is a bundle of modules: the app gives it a short name, and the path says where it is. The lib package holds what Safari Ride shares with the live pictures in this book, such as what a shape is and what a key press is.
A module is one file of Roc, named for what it holds. SafariApp.roc holds the SafariApp module, and import SafariApp brings it in. import lib.WasmApp brings in the WasmApp module from the lib package.
The brackets after app name the two things the app hands the platform, and the last two lines define them. Model is the type of what the app keeps as it runs, and here it's the one in SafariApp.
SafariApp.canvas_app is the whole ride as one value, and the WasmApp.program function fits it to the web platform. Its result is the app's program. The “The platform” chapter, near the end, opens that value. The next chapter reads the types inside those modules.