A first NeoHaskell project
Neo is the command-line tool for working with NeoHaskell projects. It creates the initial files, builds your code, and runs the application. We’ll start by using it to create a project called my-neo-app.
A new project already contains a small application called Counter. It lets you create a named counter, add or subtract an amount, and read its current value. Having an application in place gives us something to run and examine before changing any code.
You’ll need Neo, Nix, and Git installed to follow along. Install NeoHaskell covers the required tools.
Creating a project with Neo
Section titled “Creating a project with Neo”Neo puts each new project in its own directory. If you keep your code in a directory named projects, open a terminal there. The command below creates projects/my-neo-app; there is no need to make the my-neo-app directory yourself.
There are two ways to answer Neo’s setup questions. You can answer them in the terminal, or let an agent use the defaults. Choose one route for this project.
For the interactive route, run:
neo new my-neo-appNeo asks for the project name, version, description, author, and license. The name is already filled in as my-neo-app. Keep the starting version of 0.1.0, and enter a description and author name. Press Enter after each answer. At the license question, the up and down keys change the selection; MIT is the default.
If your agent is creating the project, it can skip those questions with --ci:
neo new my-neo-app --ciThis uses version 0.1.0, description A new NeoHaskell project, author Anonymous, and license MIT. The resulting application is the same. The answers become project metadata, which you can change later in neo.json.
For example, you can give your agent this task from the directory where the project belongs:
Use
neo new my-neo-app --cito create a project here. Leave the Counter application unchanged, and don’t start it yet. Show me the generated neo.json.
After either route finishes, my-neo-app contains the generated files. Neo also makes an initial Git commit, recording the project as it was created. If a directory with that name already exists, Neo stops rather than overwriting it. A different name creates a separate project.
The project settings
Section titled “The project settings”Let’s look at what Neo has created. Enter the new directory and open it in your editor:
cd my-neo-appThe neo.json file holds the project’s settings. Near the beginning are its name and version:
{ "name": "my-neo-app", "version": "0.1.0"}We’ve left out the other fields here so we can look at these two first. name is the project name you supplied to Neo. version belongs to your application; it is not the version of NeoHaskell.
The file also has a field named neo-version. Its value is a long string of letters and numbers identifying an exact revision of the NeoHaskell framework code. This is called a pin. New changes to NeoHaskell do not change the code your project uses, because it still points to that revision. We can leave the pin as it is while working with the starter.
Neo uses these settings to generate the build configuration. Changes to project settings belong in neo.json rather than in the generated files.
The Counter application
Section titled “The Counter application”The source code is in src/. Open src/App.hs, then look for the src/Starter/Counter/ directory. Although we named the project my-neo-app, its example application is still called Counter. The project name does not rename the example.
Counter uses the same parts introduced in Commands and queries. A command asks to create a counter or change its value. An event records an accepted change, and a query supplies the current counter values. These are the same jobs that placing an order and checking stock perform in the mug-shop example, with less business behavior to work through.
For now, we’re keeping that code unchanged. This gives us a starting application whose behavior we can check before asking an agent to add anything.