Skip to content

aantron/reason-native-hello

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Reason Native Hello!


This is the smallest possible Reason Native project that still uses all the latest tools. Run:

git clone https://github.com/aantron/reason-native-hello.git
cd reason-native-hello
npx esy install
npx esy dune exec ./hello.exe

This will print

Hello, world!

The first time you run this, esy will take several minutes building all the dependencies. After that, esy will use its cache.


That's it!




Explanation

We are using four tools here:

  1. Reason will parse our hello.re file for the compiler.

  2. The OCaml compiler will translate our parsed hello.re into native code.

  3. Dune is the OCaml build system. It knows how to call into Reason and OCaml in the right way to build our executable. We tell it what we want in the dune file:

    (executable
     (name hello))
    
  4. esy is a package manager that is able to download, build, and organize all of the above. Hence, esy.json looks like this:

    {
      "dependencies": {
        "@opam/dune": "^2.9",
        "@opam/reason": "^3.7.0",
        "ocaml": "^4.12"
      }
    }

    The @opam/*** package namespace refers to the opam package repository, which has a lot of useful OCaml packages. However, we are not using the opam tool — we are using esy itself to connect directly to the opam repository.

    Reason libraries are also found in npm. However, many of those target JavaScript — though some target both or only native code.


Next steps

  • You can make a multi-file project by just creating another .re file, for example another.re:

    let how_are_you =
      "amazing!"

    You can refer to this value from hello.re like this:

    let () =
      print_endline(Another.how_are_you)
  • To depend on a library, say Dream, tell esy to download it:

    {
      "dependencies": {
        "@opam/dream": "*",
        "@opam/dune": "^2.9",
        "@opam/reason": "^3.7.0",
        "ocaml": "^4.12"
      }
    }

    ...and run npx esy install. Then, tell Dune to link it:

    (executable
     (name hello)
     (libraries dream))
    
  • If you are using Visual Studio Code, download the OCaml Platform extension to get syntax highlighting, type hints, etc. Add this to esy.json:

      "devDependencies": {
        "@opam/ocaml-lsp-server": "*"
      }

    and re-run npx esy install.

  • Reason can do a lot more. If you want to do full-stack Reason, meaning compile Reason to both native code and JavaScript, get ReScript, which can compile Reason code to JS, and see the Full-stack ReScript example.


Contact

Visit the Reason Discord and chat with us! Edits/suggestions for this repo are welcome. Open an issue, PR, or highlight @antron in Discord!

About

The smallest possible Reason Native project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages