816 search results for ""

Showing 751 - 800
  1. A Tour of OCaml

    Conclusion

    In this tutorial, OCaml was used interactively. The next tutorial, Your First OCaml Program , shows you how to write OCaml files, how to compile them, and how to kickstart a project. <!-- 1. Values

    First Steps
  2. Your First OCaml Program

  3. Your First OCaml Program

    Working Within an opam Switch

    When you work on several OCaml projects simultaneously, you should create more opam switches. For instructions on how to do that, see Introduction to opam Switches . When you installed OCaml, a glo

    First Steps
  4. Your First OCaml Program

    Compiling OCaml Programs

    In the rest of this tutorial, we will make more changes to this project in order to illustrate OCaml's tooling. Voilà! You've just written your first OCaml program. Let's see what happens when we

    First Steps
  5. Your First OCaml Program

    Watch Mode

    Before we dive in, note that you will typically want to use Dune's watch mode to continually compile and optionally restart your program. This ensures that the language server has the freshest poss

    First Steps
  6. Your First OCaml Program

    Why Isn't There a Main Function?

    However, it is common practice to single out a value that triggers all the side effects and mark it as the intended main entry point. In OCaml, that's the role of let () = , which evaluates the expr

    First Steps
  7. Your First OCaml Program

    Modules and the Standard Library, Cont'd

    This replaces the function print_endline with the function printf from the Printf module in the standard library. Building and executing this modified version should produce the same output as

    First Steps
  8. Your First OCaml Program

    Every File Defines a Module

    <!-- FIXME refer to Dune/Library tutorial when available --> Note : If you add a file named hello.ml in the lib directory, Dune will consider this the whole Hello module and it will make En

    First Steps
  9. Your First OCaml Program

    Defining Module Interfaces

    This is because we haven't changed lib/en.mli . Since it does not list hello , it is therefore private. Trying to compile this fails. Also edit the bin/main.ml file like this: Amend the

    First Steps
  10. Your First OCaml Program

    Defining Multiple Modules in a Library

    A more detailed introduction to modules can be found at Modules . Finally, run dune build and dune exec hello to see the new output, using the modules you just created in the hello librar

    First Steps
  11. Your First OCaml Program

    Installing and Using Modules From a Package

    Finally, execute as before: Fun fact : Dune configuration files are S-expressions. Before the example will build and run, you need to tell Dune that it needs Sexplib to compile the project. D

    First Steps
  12. Your First OCaml Program

    Using the Preprocessor to Generate Code

    Here is the result: Finally, you'll also need to edit bin/main.ml v has the type string list . We're using String.split_on_char to turn a string into a string list by splitting the str

    First Steps
  13. Your First OCaml Program

    A Sneak-Peek at Dune as a One-Stop Shop

    The _build directory is where Dune stores all the files it generates. It can be deleted at any time, but subsequent builds will recreate it. Running tests Generating documentation Producing packag

    First Steps
  14. Your First OCaml Program

    Minimum Setup

    Note : minimo.exe is not a file name. This is how Dune is told to compile the minimo.ml file using OCaml's native compiler instead of the bytecode compiler. As a fun fact, note that an empty file

    First Steps
  15. Your First OCaml Program

    Conclusion

    <!-- TODO: link Project Quickstart If you're already familiar with lists, maps, and folds, and need to be productive as fast as possible, dive into the “Project Quickstart” guide. --> This tuto

    First Steps
  16. Configuring Your Editor

  17. Configuring Your Editor

    Visual Studio Code

    Upon first loading an OCaml source file, you may be prompted to select the toolchain in use. Pick the version of OCaml you are using, e.g., 5.1.0 from the list. For VSCode, install the OCaml Pl

    Tooling
  18. Configuring Your Editor

    Editor Features at Your Disposal

    If your editor is setup correctly, here are some important features you can begin using to your advantage:

    Tooling
  19. Configuring Your Editor

    1) Hovering for Type Information

    This is a great feature that lets you see type information of any OCaml variable or function. All you have to do is place your cursor over the code and it will be displayed in the tooltip. VSCode Ho

    Tooling
  20. Configuring Your Editor

    2) Jump to Definitions With Ctrl + Click

    If you hold down the Ctrl key while hovering, the code appears as a clickable link which if clicked takes you to the file where the implementation is. This can be great if you want to understand ho

    Tooling
  21. Configuring Your Editor

    3) OCaml Commands With Ctrl + Shift + P

    Pressing the key combination Ctrl + Shift + P opens a modal dialog at the top. If you type the word ocaml , you will be presented with a list of various OCaml commands at your disposal which can b

    Tooling
  22. Configuring Your Editor

    Windows Users

    If you used the DkML distribution, you will need to: 1. Go to File > Preferences > Settings view (or press Ctrl , ) 2. Select User > Extensions > OCaml Platform 3. Uncheck OCaml: U

    Tooling
  23. Configuring Your Editor

    Emacs

    A major mode, which, among other things, supports syntax highlighting and the structuring of indentation levels A minor mode, which will interact with a language server (such as ocaml-lsp-server ).

    Tooling
  24. Configuring Your Editor

    Choosing a major mode

    For the purposes of this tutorial, we are going to focus on the use of tuareg as the major mode, but you should feel free to experiment and choose your favourite one! To use tuareg , you can add

    Tooling
  25. Configuring Your Editor

    MELPA and use-package

    The use-package macro has been built into Emacs since version 29.1. If you are on an older version, you will need to install use-package manually. You will also need to ensure MELPA is configur

    Tooling
  26. Configuring Your Editor

    LSP setup for OCaml

    You are now ready to edit OCaml code productively with Emacs! And that's all there is to it! Now all you need to do is install ocaml-lsp-server and ocamlformat in your switch : Next, we

    Tooling
  27. Configuring Your Editor

    Finer configuration

    OCaml-eglot can be finely configured, the project README gives several configuration paths to adapt perfectly to your workflow. You will also find there an exhaustive presentation of the different

    Tooling
  28. Configuring Your Editor

    Getting Type Information

    OCaml-eglot README provides a comprehensive overview of all the functions available in this mode! Emacs Type information Opening an OCaml file should launch an ocaml-lsp server, and you can con

    Tooling
  29. Configuring Your Editor

    Structured Navigation and Editing

    Note: For advanced structured navigation and editing, OCaml is supported by Combobulate . It leverages tree-sitter to provide syntax-aware movement and structural editing for your OCaml code. You

    Tooling
  30. Configuring Your Editor

    Vim

    After installing Merlin above, instructions will be printed on how to link Merlin with your editor. If you do not have them visible, just run this command: For Vim, we won't use the LSP server b

    Tooling
  31. Configuring Your Editor

    Talking to Merlin

    Tooling
  32. Configuring Your Editor

    Getting Type Information in Vim

    In the Vim editor, press Esc to enter command mode. Place the cursor over the variable. Type :MerlinTypeOf and press Enter . The type information will be displayed in the command bar. Other Me

    Tooling
  33. Configuring Your Editor

    Neovim

    A newer, more recommended way is to use the new Neovim LSP API for versions newer than v0.11.0 via vim.lsp . A more traditional way is to use nvim-lspconfig . For more info, kickstart.nvim has a

    Tooling
  34. Configuring Your Editor

    Using vim.lsp

    See :h lsp-config for more detail on configuration options. Add this to your toplevel init.lua .

    Tooling
  35. Configuring Your Editor

    Using vim.lsp With runtimepath

    Then enable them in the toplevel init.lua . Add your LSP config to lsp/ocamllsp.lua . Your Neovim config should have the following structure now. Run the following at the root of your config

    Tooling
  36. Configuring Your Editor

    Using nvim-lspconfig

    There is no need to pass more settings to setup because nvim-lspconfig provides reasonable defaults. See the nvim-lspconfig OCaml LSP configuration for more info. Add this to your nvim-lspco

    Tooling
  37. Introduction to the OCaml Toplevel

  38. Introduction to the OCaml Toplevel

    Using Packages in UTop

    Tooling
  39. Introduction to the OCaml Toplevel

    Loading Libraries In UTop

    Tip : UTop knows about the available libraries and completion works. Outside utop you can use ocamlfind list to display the complete list of libraries. Note that opam package may bundle several l

    Tooling
  40. Introduction to the OCaml Toplevel

    Using a Pre-Processor Extension (PPX) in UTop

    Assuming that the ppx_deriving package is installed in your opam switch, you run #require "ppx_deriving.show" . To activate a PPX in UTop, all you need to do is to load the corresponding library.

    Tooling
  41. Introduction to opam Switches

  42. Introduction to opam Switches

    What Is an opam Switch?

    After installing OCaml , you'll have a single switch called default . An opam switch is an isolated OCaml environment. Each switch has its own OCaml compiler, installed packages, and binaries, all

    Tooling
  43. Introduction to opam Switches

    Global vs. Local Switches

    For project work, we recommend local switches. They keep each project's dependencies isolated and make it easy for collaborators to reproduce your setup. Global switches are named environments sto

    Tooling
  44. Introduction to opam Switches

    Listing Available Compilers

    Omitting ocaml-base-compiler shows the full list, including compiler variants with additional options enabled (such as flambda optimisations or frame pointers). These are useful for advanced use ca

    Tooling
  45. Introduction to opam Switches

    Creating a Global Switch

    This switch is now available from any directory. It lives at ~/.opam/my_switch/ . Then update your shell environment: To create a named global switch:

    Tooling
  46. Introduction to opam Switches

    Creating a Local Switch

    If the directory contains .opam files, opam will automatically install their dependencies into the new switch. You can prevent this with opam switch create . 5.4.1 --no-install . This creates an

    Tooling
  47. Introduction to opam Switches

    Listing Your Switches

    The arrow -> marks the currently active switch:

    Tooling
  48. Introduction to opam Switches

    Switching Between Switches

    Local switches are selected automatically when you cd into their directory, but you still need to run eval $(opam env) (or use a tool like direnv ) to update the shell environment. The eval $(

    Tooling
  49. Introduction to opam Switches

    Removing a Switch

    Learn more about opam switches in the opam manual and the opam-switch reference . For a local switch, use the path: To delete a switch you no longer need:

    Tooling
  50. OCaml on Windows