652 search results for ""

Showing 351 - 400
  1. Objects

    Objects and Classes

    s is an opaque object. The implementation (i.e., the list) is hidden from callers. In the OCaml toplevel, we can examine the types of objects and methods in more detail: Notice the syntax.

    Advanced Topics
  2. Modules

    Abstract and Read-Only Types

    Abstract and read-only types can be either variants, as shown in this section, records, or aliases. It is possible to access a read-only record field's value, but creating such a record requires usin

    Module System
  3. Higher Order Functions

    Mapping Custom Data Types

    Note that the structure of the tree is preserved, but every time we encounter a value , we update it with (fn value) . When working with our custom data types, such as the tree we used in the

    Introduction
  4. OCaml Programming Guidelines

    Naming Anonymous Functions

    Justification : Much clearer, in particular if the name given to the function is meaningful. <!-- $MDX skip --> write <!-- $MDX skip --> In the case of an iterator whose argument is a comp

    Resources
  5. Hash Tables

    Adding Data to a Hash Table

    Where the type used to be the polymorphic (_weak1, _weak2) , it now has the concrete representation of (string * string) . Now that we put data into my_hash , lets look at its type: The retu

    Data Structures
  6. Functors

    Write a Functor to Extend Modules

    Modules Array and List appear augmented with Array.scan_left and List.scan_left . For brevity, the output of the first two toplevel commands is not shown here. Run the dune utop command. O

    Module System
  7. OCaml on Windows

    Visual Studio Code (VSCode)

    If you use WSL2 , you will remotely connect to your WSL2 instance from VSCode. Microsoft has a useful blog post that covers getting WSL2 and Visual Studio Code connected. Go to File > Prefer

    Resources
  8. Values and Functions

    Local Definitions

    In both examples, d and e are local definitions. Arbitrary combinations of chaining or nesting are allowed. e is bound to 6 inside e * 5 d is bound to 30 inside d * 7 Here is how scopin

    Introduction
  9. Basic Data Types and Pattern Matching

    Introduction

    <!-- The goal of this tutorial is to provide for the following capabilities: - Handle data of all predefined types using dedicated syntax - Write variant type definitions: simple, recursive, and poly

    Introduction
  10. Introduction to opam Switches

    Local Switches

    Local switches are automatically selected based on the current working directory. When you navigate into a directory with an associated local switch, opam uses that switch for any OCaml-related opera

    Tooling
  11. OCaml Programming Guidelines

    Usage in Module Interface

    The function's usage must appear in the module's interface that exports it, not in the program that implements it. Choose comments as in the OCaml system's interface modules, which will subsequen

    Resources
  12. Basic Data Types and Pattern Matching

    User-Defined Types

    These three kinds of type definitions are covered in the next three sections. Variant Record Aliases User-defined types are always introduced using the type … = … statement. The keyword type

    Introduction
  13. Transitioning to Multicore with ThreadSanitizer

    A Proposed Workflow

    Install TSan Write a parallel test runner Run tests under TSan If TSan complains about data races, address the reported issue and go to step 2. Now if we want to see if this code is Multicore read

    Guides
  14. Sequences

    A Mental Model for Seq.Cons vs Seq.cons

    It useful to think of Seq.Cons and Seq.cons as accomplishing different tasks. Seq.Cons provides a convenient means of recursively defining a sequence generator and a clumsy means to prepend

    Data Structures
  15. Values and Functions

    Function as Values

    <!-- For now, let's put aside those definitions and instead start playing with functions. Their meaning will arise from experience. Once things make sense, using these terms is just a means to interac

    Introduction
  16. Error Handling

    Asynchronous Exceptions

    The latter is thrown when the user interrupts an interactive execution. Because they are loosely or not at all related with the program logic, it mostly doesn't make sense to track the place wher

    Guides
  17. Debugging

    Detecting a Data Race

    The TSan instrumentation benefits from compiling programs with debug information, which happens by default under dune . To manually invoke the ocamlopt compiler under our 5.2.0+tsan switch

    Guides
  18. Higher Order Functions

    Async code

    <!-- Comment let f () = 42 let f () = g 42 One possible issue with the repeat function used in the intro comes from the fact it is used to produce a side effect. Therefore two stories get mixed tog

    Introduction
  19. Configuring Your Editor

    1) Hovering for Type Information

    This is a great feature that let's 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 H

    Tooling
  20. 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 co

    Tooling
  21. Sequences

    Reading a File with Seq.Unfold

    Note: production code should handle file opening errors, this example has been kept short to focus only on how files relate to sequences. Finally, let's read the file's contents using Seq.unfol

    Data Structures
  22. Installing OCaml

    Install Platform Tools

    You're now all set and ready to start hacking. All these tools can be installed using a single command: UTop , a modern interactive toplevel (REPL: Read-Eval-Print Loop) Dune , a fast and full-fe

    First Steps
  23. Hash Tables

    Finding Data in Hash Tables

    This returns ["hard"; "hug"; "hi"; "hello"] , demonstrating that hashed key collisions are associated with a list of values associated with that key. However, the previous values associated wit

    Data Structures
  24. Configuring Your Editor

    Getting Type Information

    In the Vim editor, press the 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. Othe

    Tooling
  25. Operators

    Using Binary Operators

    Finally, in the third line, all the arguments expected by List.filter are provided. The returned list contains the values satisfying the ( <= ) 10 function. The first shows the List.filter typ

    Advanced Topics
  26. Error Handling

    Safe vs. Unsafe Functions

    The main ways to write such safe error-handling functions are to use either option (next section) or result (following section) values. Although handling errors in data using those types may a

    Guides
  27. Managing Dependencies With opam

    Installing a Git Dependency in Your Switch

    You can install a package in your active switch directly from a Git URL:

    Projects
  28. Functors

    Extending a Module with a Standard Library Functor

    This allows the user to seemingly extend the module String with a submodule Set . Check the behaviour using opam exec -- dune exec funkt < dune . funkt.ml Using the include statement, here i

    Module System
  29. OCaml Programming Guidelines

    Using Tab Stops

    Justification : Between one display and another, the indentation of the program changes completely. It can also become completely wrong if the programmer used both tabulations and spaces to inden

    Resources
  30. Values and Functions

    Anonymous Functions

    Anonymous functions are often passed as arguments to other functions. The identity function, which takes anything and returns it unchanged The square function, which takes an integer and returns it

    Introduction
  31. Values and Functions

    Defining Global Functions

    The former explicitly binds the anonymous function to a name. The latter uses a more compact syntax and avoids the fun keyword and the arrow symbol. The expression, which happens to be a function

    Introduction
  32. Formatting and Wrapping Text

    A Concrete Example

    <!-- $MDX skip --> In Caml Light, replace the first line by: I use the format library to print the lambda-terms: First, I give the abstract syntax of lambda-terms: Thus the problem is to pr

    Tutorials
  33. Basic Data Types and Pattern Matching

    Function Parameter Aliases

    This is useful for matching variant values of parameters. Function parameters can also be given a name with pattern matching for tuples and records.

    Introduction
  34. 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 our switch : Next, we n

    Tooling
  35. Modules

    Displaying a Module's Interface

    If you are using Dune, .cmi file are in the _build directory. Otherwise, you can compile manually to generate them. The command ocamlc -c cairo.ml will create cairo.cmo (the executable byteco

    Module System
  36. OCaml Programming Guidelines

    Formatting Guidelines

    Pseudo spaces law : never hesitate to separate words in your programs with spaces. The space bar is the easiest key to find on the keyboard, so press it as often as necessary! If you choose no

    Resources
  37. Introduction to opam Switches

    Global Switches

    Opam's system switch is a global switch that is associated with the OCaml installation on your operating system. The system switch is accessible across the entire system. When creating an opam swi

    Tooling
  38. Profiling

    Polymorphic Types

    Lesson: if you have a function which is unintentionally polymorphic then you can help the compiler by specifying types for one or more of the arguments. Disappointingly although the definition o

    Guides
  39. Objects

    Inheritance and Coercions

    Is it possible to coerce from a superclass (e.g., widget ) to a subclass (e.g., button )? The answer, perhaps surprisingly, is NO! Coercing in this direction is unsafe . You might try to coerc

    Advanced Topics
  40. Installing OCaml

    Initialise opam

    Any problems installing? Be sure to read the latest release notes . You can file an issue at <https://github.com/ocaml/opam/issues> or <https://github.com/ocaml-windows/papercuts/issues> . Note

    First Steps
  41. Monads

    Composition and Monad Laws

    In that formulation, it becomes immediately clear that return is a left and right identity, and that composition is associative. Law 1: return >=> f behaves the same as f . Law 2: f >=> r

    Data Structures
  42. OCaml Programming Guidelines

    Arithmetic operators: the same rules as in mathematics

    For example: 1 + 2 * x means 1 + (2 * x) .

    Resources
  43. OCaml Programming Guidelines

    How to Choose Identifiers

    It's hard to choose identifiers whose name evokes the meaning of the corresponding portion of the program. This is why you must devote particular care to this, emphasising clarity and regularity

    Resources
  44. Fix Homebrew Errors on Apple M1

    Disable Rosetta

    It's essential to close your current Terminal window and open a new one for it to work properly. Then run the following command. If you get the output shown, you're ready to brew! Close Terminal

    Resources
  45. OCaml Programming Guidelines

    Managing Program Development

    Below are tips from veteran OCaml programmers that have served in developing the compilers. These are good examples of large, complex programs developed by small teams.

    Resources
  46. Formatting and Wrapping Text

    Principles

    boxes : a box is a logical pretty-printing unit, which defines a behaviour of the pretty-printing engine to display the material inside the box. break hints : a break hint is a directive to the p

    Tutorials
  47. Values and Functions

    What is a Value?

    An expression's type (before evaluation) and its resulting value's type (after computation) are the same. This allows the compiler to avoid runtime type checks in binaries. In OCaml, the compiler rem

    Introduction
  48. OCaml Programming Guidelines

    Sequences inside branches of if

    <!-- $MDX skip --> In the same way, a sequence which appears in the then or else part of a conditional must be delimited:

    Resources
  49. OCaml Programming Guidelines

    Height of the Page

    Justification : When a function goes beyond one screenful, it's time to divide it into subproblems and handle them independently. Beyond a screenful, one gets lost in the code. The indentation is

    Resources
  50. Debugging

    Installing a TSan Switch

    If the above fails during installation of conf-unwind with No package 'libunwind' found , try setting the environment variable PKG_CONFIG_PATH to point to the location of libunwind.pc , for

    Guides