Skip to content

Commit 0106436

Browse files
committed
tmp: complete the implement part
Signed-off-by: Etienne Marais <[email protected]>
1 parent fb8be06 commit 0106436

File tree

9 files changed

+143
-1
lines changed

9 files changed

+143
-1
lines changed
Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,119 @@
11
# Implement a Library Parameter
22

3-
TODO @maiste: create an implementation of the interface.
3+
Now we have our parameter, we have our interface. In this section, we will
4+
focus on how to write a library that implements our parameter.
5+
6+
## Structure Our Project
7+
8+
As we did for the `parameter`, we are going to add a new directory in our
9+
structure under `lib/`. We create a directory `impl/` under `lib/`. At the root
10+
of the directory, we can run:
11+
12+
```{code-block} shell
13+
mkdir -p lib/impl
14+
```
15+
16+
## Creating an Implementation
17+
18+
As for the parameter, we start with our `lib/impl/dune`
19+
20+
:::{literalinclude} implement/lib/impl/dune
21+
:language: dune
22+
:::
23+
24+
To create an implementation, we write two files:
25+
- The first one is `lib/impl/impl.ml`. It contains the implementation.
26+
- The second one is `lib/impl/impl.mli`. It contains the interface of the implementation.
27+
28+
29+
We define an additional `create` function to send our element in the abstract
30+
type. Our project now implements a parameter.
31+
32+
:::{literalinclude} implement/lib/impl/impl.mli
33+
:language: ocaml
34+
:::
35+
36+
To have an abstract interface, we define an `mli` file.
37+
38+
:::{literalinclude} implement/lib/impl/impl.ml
39+
:language: ocaml
40+
:::
41+
42+
43+
## Conclusion
44+
45+
In this section you have learned to implement a parameter. We have added the
46+
following files:
47+
48+
::::{dropdown} `lib/impl/dune`
49+
:icon: file-code
50+
51+
:::{literalinclude} implement/lib/impl/dune
52+
:language: dune
53+
:::
54+
::::
55+
56+
::::{dropdown} `lib/impl/impl.ml`
57+
:icon: file-code
58+
59+
:::{literalinclude} implement/lib/impl/impl.ml
60+
:language: ocaml
61+
:::
62+
::::
63+
64+
::::{dropdown} `lib/impl/impl.mli`
65+
:icon: file-code
66+
67+
:::{literalinclude} implement/lib/impl/impl.mli
68+
:language: ocaml
69+
:::
70+
::::
71+
72+
You should also have the previous files:
73+
74+
::::{dropdown} `dune-project`
75+
:icon: file-code
76+
77+
:::{literalinclude} implement/dune-project
78+
:language: dune
79+
:::
80+
::::
81+
82+
::::{dropdown} `test/dune`
83+
:icon: file-code
84+
85+
:::{literalinclude} implement/test/dune
86+
:language: dune
87+
:::
88+
::::
89+
90+
::::
91+
92+
::::{dropdown} `test/simple.t`
93+
:icon: file-code
94+
95+
:::{literalinclude} implement/test/simple.t
96+
:language: dune
97+
:::
98+
::::
99+
100+
::::{dropdown} `lib/param/dune`
101+
:icon: file-code
102+
103+
:::{literalinclude} implement/lib/param/dune
104+
:language: dune
105+
:::
106+
::::
107+
108+
::::{dropdown} `lib/param/param.mli`
109+
:icon: file-code
110+
111+
:::{literalinclude} implement/lib/param/param.mli
112+
:language: ocaml
113+
:::
114+
::::
115+
116+
::::
117+
118+
In the next section, we will see how we write a library parameterized by our
119+
parameter.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(lang dune 3.20)
2+
(using oxcaml 0.1)
3+
4+
(package (name param))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(library
2+
(name impl)
3+
(implement param))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type t = int
2+
3+
let create t = t
4+
5+
let compare = Int.compare
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type t
2+
val create : int -> t
3+
val compare: t -> t -> int
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(library_parameter
2+
(public_name param))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type t
2+
val compare: t -> t -> int
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(cram
2+
(applies_to :whole_subtree)
3+
(enabled_if %{oxcaml_supported}))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This test shows the behavior of the program and ensures it does not have
2+
regression.
3+
4+
$ dune build

0 commit comments

Comments
 (0)