From df8c0d9eddbe11efb2051f95d24b5c58efb55b31 Mon Sep 17 00:00:00 2001
From: Richard Feldman
Let's move out of the REPL and create our first Roc application!
Make a file named main.roc
and put this in it:
In order to get access to this examples/cli/cli-platform/main.roc
file, you'll need
-to download the Roc source code repository, which
-contains the examples
directory at the beginning of that string. Make sure you cd
-into that directory once you've downloaded it!
Try running this with:
roc dev -You should see this printed to the terminal:
+You should a see a message about a file being downloaded, followed by this:
I'm a Roc application!Congratulations - you've written your first Roc application! We'll go over what the parts of
this file above main
do later, but first let's play around a bit.
Now if you run roc dev
, you should see this:
Now run roc dev
again. This time the "Downloading …" message won't appear;
+the file has been cached from last time, and won't need to be downloaded again.
You should see this:
There are 5 animals.main.roc
now has four definitions—defs for
short—birds
, iguanas
, total
, and main
.
total
is calling addAndStringify
passing a rec
By the way, this is a comment in Roc:
# The `name` field is unused by addAndStringify -Whenever you write # it means that the rest of the line is a comment, -and will have no effect on the program.
+Whenever you write #
it means that the rest of the line is a comment,
+and will not affect the program.
Comments that begin with ##
will be included in generated documentation (roc docs
). They can include code blocks by adding five spaces after ##
.
Roc does not have multiline comment syntax.
Roc has a couple of shorthands you can use to express some record-related operations more concisely.
Instead of writing \record -> record.x
we can write .x
and it will evaluate to the same thing:
@@ -1231,7 +1237,7 @@ Roc compiler. That's why they're called "builtins!"
Let's take a closer look at the part of main.roc
above the main
def:
roc dev
, the Roc compiler will build an executab
named hello
(or hello.exe
on Windows) and run it. You can also build the executable
without running it by running roc build
.
The remaining lines all involve the platform this application is built on:
-packages { pf: "examples/cli/cli-platform/main.roc" } -imports [pf.Stdout] -provides main to pf +packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.1.0/_V6HO2Dwez0xsSstgK8qC6wBLXSfNlVFyUTMg0cYiQQ.tar.br" } +imports [pf.Stdout] +provides [main] to pf -The packages { pf: "examples/cli/cli-platform/main.roc" }
part says two things:
The packages { pf: "https://…tar.br" }
part says three things:
"examples/cli/cli-platform/main.roc"
"https://…tar.br"
.tar.br
file extension).
+Once the file has been downloaded, its contents will be verified against this hash, and it will only
+be installed if they match. (This way, you can be confident the download was neither corrupted nor
+changed since it was originally published.)pf
so we can refer to it more concisely in the future.The imports [pf.Stdout]
line says that we want to import the Stdout
module
@@ -1263,34 +1274,15 @@ at that again:
line
which is exposed by a module named
Stdout
.
When we write imports [pf.Stdout]
, it specifies that the Stdout
-module comes from the pf
package.
Since pf
was the name we chose for the examples/cli/cli-platform/main.roc
-package (when we wrote packages { pf: "examples/cli/cli-platform/main.roc" }
),
-this imports
line tells the Roc compiler that when we call Stdout.line
, it
-should look for that line
function in the Stdout
module of the
-examples/cli/cli-platform/main.roc
package.
pf
in the packages { pf: … }
section.
If we would like to include other modules in our application, say AdditionalModule.roc
and AnotherModule.roc
, then they can be imported directly in imports
like this:
Comments that begin with ##
will be included in generated documentation (roc docs
). They require a single space after the ##
, and can include code blocks by adding five spaces after ##
.
Roc also supports inline comments and line comments with #
. They can be used to add information that won't be included in documentation.
Roc does not have multiline comment syntax.
Tasks are technically not part of the Roc language, but they're very common in
-platforms. Let's use the CLI platform in examples/cli/cli-platform/main.roc
as an example!
In the CLI platform, we have four operations we can do:
+platforms. Let's continue using the basic-cli platform +we've been using up to this point as an example! +In the basic-cli
platform, we have four operations we can do:
examples/cli/cli-platform/main.ro
- Read a string from a file
We'll use these four operations to learn about tasks.
-First, let's do a basic "Hello World" using the tutorial app.
+Let's start with a basic "Hello World" program.
app "cli-tutorial" - packages { pf: "examples/cli/cli-platform/main.roc" } + packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.1.0/_V6HO2Dwez0xsSstgK8qC6wBLXSfNlVFyUTMg0cYiQQ.tar.br" } imports [pf.Stdout] provides [main] to pf @@ -1325,7 +1317,7 @@ when it runs (hence the*
).
Let's change main
to read a line from stdin
, and then print it back out again:
This works, but we can make it a little nicer to read. Let's change it to the following:
app "cli-tutorial" - packages { pf: "examples/cli/cli-platform/main.roc" } + packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.1.0/_V6HO2Dwez0xsSstgK8qC6wBLXSfNlVFyUTMg0cYiQQ.tar.br" } imports [pf.Stdout, pf.Stdin, pf.Task.{ await }] provides [main] to pf