# \[ocaml\] Interface Lcon

**URL:** <https://discuss.ocaml.org/t/ocaml-interface-lcon/1710>\
**Category:** Learning\
**Created:** [March 14, 2018, 2:43pm UTC](https://discuss.ocaml.org/t/ocaml-interface-lcon/1710 "2018-03-14T14:43:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![herosgogo](https://avatars.discourse-cdn.com/v4/letter/h/3ec8ea/32.png) [@herosgogo](https://discuss.ocaml.org/u/herosgogo)\
**Post date:** [March 14, 2018, 2:43pm UTC](https://discuss.ocaml.org/t/ocaml-interface-lcon/1710/1 "2018-03-14T14:43:49Z")

</div>

Hi everyone,  
I have started Ocaml for 3 months, and I have one question about interface in ocaml.

Now I define a record type in the interface Lcon:  
lcon.ml: type t ={a:bool;b:int}  
lcon.mli: type t

If I want to create a variable of type t in an other file:  
ex.ml : let lo = {Lcon.a=true;Lcon.b=3};;  
---------\>Is this correct?

Thank you in advance

---

<div class="post-metadata">

**Author:** ![ivg](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ivg/32/103_2.png) [@ivg](https://discuss.ocaml.org/u/ivg)\
**Post date:** [March 14, 2018, 4:03pm UTC](https://discuss.ocaml.org/t/ocaml-interface-lcon/1710/2 "2018-03-14T16:03:19Z")

</div>

Since in the interface, you have defined `type t` as an abstract type, you can’t create a value of this type using the record syntax, since the fact that it is a record is now a hidden implementation detail (that’s the whole purpose of the abstract type).

If you do not want to make it abstract, then you need to declare it in the mli file the same as you did in the ml fie:

```auto
lcon.mli: type t ={a:bool;b:int}

```

This will reveal the implementation and will allow users of your module to construct values directly, here are some syntaxes that are acceptable by OCaml:

```auto
{Lcon.a=true; Lcon.b=3}
{Lcon.a=true; b=3} 
Lcon.({a=true; b=3})

```

---

<div class="post-metadata">

**Author:** ![kantian](https://avatars.discourse-cdn.com/v4/letter/k/4bbf92/32.png) [@kantian](https://discuss.ocaml.org/u/kantian)\
**Post date:** [March 14, 2018, 4:27pm UTC](https://discuss.ocaml.org/t/ocaml-interface-lcon/1710/3 "2018-03-14T16:27:28Z")

</div>

> [@ivg](#):
>
> here are some syntaxes that are acceptable by OCaml

Beware that the third syntax can lead to subtle bugs due to type directed disambiguation:

```ocaml
module type S = sig type t = {a:bool; b:int} end

module M : S = struct type t = {a:bool; b:int} end

module N : S = struct type t = {a:bool; b:int} end

(* 
  here the fields `a` and `b' in the right hand side are
  interpreted as M.a and M.b due to type directed disambiguation.
*)
M.({a=true;b=3}) = N.({a=true;b=3})
- : bool = true

(* compare to this type-checking error *)
{M.a = true; b = 3} = {N.a = true; b = 3};;
Error: The field N.a belongs to the record type N.t
       but a field was expected belonging to the record type M.t

```

---

<div class="post-metadata">

**Author:** ![herosgogo](https://avatars.discourse-cdn.com/v4/letter/h/3ec8ea/32.png) [@herosgogo](https://discuss.ocaml.org/u/herosgogo)\
**Post date:** [March 14, 2018, 4:52pm UTC](https://discuss.ocaml.org/t/ocaml-interface-lcon/1710/4 "2018-03-14T16:52:03Z")

</div>

Thanks a lot, you saved me.

---

<div class="post-metadata">

**Author:** ![bobbypriambodo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bobbypriambodo/32/179_2.png) [@bobbypriambodo](https://discuss.ocaml.org/u/bobbypriambodo)\
**Post date:** [March 15, 2018, 6:55am UTC](https://discuss.ocaml.org/t/ocaml-interface-lcon/1710/5 "2018-03-15T06:55:32Z")

</div>

If you would rather keep the abstract type `t` on your interface (to hide the type structure implementation), you can provide a public API on lcon.mli:

```auto
type t
val create : bool -> int -> t

```

with the implementation in lcon.ml:

```auto
type t = {a: bool; b: int}
let create a b =
  {a; b}

```

and usage in ex.ml:

```auto
let lo = Lcon.create true 3

```
