# Bool \<-\> bool Js.t?

**URL:** <https://discuss.ocaml.org/t/bool-bool-js-t/11723>\
**Category:** Learning\
**Created:** [March 20, 2023, 10:39am UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723 "2023-03-20T10:39:25Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 20, 2023, 10:39am UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/1 "2023-03-20T10:39:25Z")

</div>

What is the difference between a `bool` and a `bool Js.t` , and how do I convert between the two ?

---

<div class="post-metadata">

**Author:** ![Chimrod](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chimrod/32/6042_2.png) [@Chimrod](https://discuss.ocaml.org/u/Chimrod)\
**Post date:** [March 20, 2023, 10:49am UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/2 "2023-03-20T10:49:43Z")

</div>

In js\_of\_ocaml, every javascript elements are _encoded_ inside a phantom type (`'a Js.t`), which allow to have a different representation for each of them. (see the table in the [js\_of\_ocaml documentation](https://ocsigen.org/js_of_ocaml/latest/manual/library) )

This type is here to declare that an element belongs to the javascript world and cannot be used with the usual functions from the standard library.

As the type does not have contructor, you cannot create `Js.t` value directly, but the module `Js` provide [some functions](https://ocsigen.org/js_of_ocaml/latest/api/js_of_ocaml/Js_of_ocaml/Js/index.html#conversion-functions-between-javascript-and-ocaml-types) in order to switch from the OCaml type to javascript and vice-versa.

(I don’t know why, but the css is broken, the documentation is still readable)

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 20, 2023, 10:59am UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/3 "2023-03-20T10:59:20Z")

</div>

1. This table is outdated as of jsoo 5.1.0 right? Since we now have OCaml string == JS string

2. Here is what I struggle with. Instead of Js.t, consider option / array / list

Given v: t, we an do: Some(t), [t], [| t |] , which allows us to do  
t → t opt, t → t list, t → t array

For the other direction, we can write functions that either succeed or throw an exception.

1. I understand the individual words but not the concept of:

> [@Chimrod](#):
>
> In js\_of\_ocaml, every javascript elements are _encoded_ inside a phantom type (`'a Js.t`), which allow to have a different representation for each of them.

What is the relation of the `'a` in `'a` and `'a Js.t` if we do not have auto converions in either direction ?

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [March 20, 2023, 11:33am UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/4 "2023-03-20T11:33:04Z")

</div>

> [@Chimrod](#):
>
> In js\_of\_ocaml, every javascript elements are _encoded_ inside a phantom type (`'a Js.t`), which allow to have a different representation for each of them. (see the table in the [js\_of\_ocaml documentation](https://ocsigen.org/js_of_ocaml/latest/manual/library) )

That’s incorrect every javascript value `'a Js.t` has the same representation: it’s a JavaScript object. The phantom type `'a` is just a trick to track what the objects offers to call on it, you can put whatever you want here. It’s a way of structurally typing JavaScript objects (a bad idea if you ask me).

---

<div class="post-metadata">

**Author:** ![Chimrod](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chimrod/32/6042_2.png) [@Chimrod](https://discuss.ocaml.org/u/Chimrod)\
**Post date:** [March 20, 2023, 11:49am UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/5 "2023-03-20T11:49:22Z")

</div>

Thanks dbuenzli your are right. I thought that some gadt magic where hidden behind in order to give a different representation.

For zeroexcuse, the point is that Js\_of\_ocaml is building a javascript code from the ocaml bytecode, and during the compilation all the types disapear. The bytecode-\>javascript compiler just get a bytecode without any information if the element is an option, a list or a tuple, but get the confirmation that all this code typechecks.

This said, we do not want automatic conversion, sometimes we are linking an external js library which requires `'a Js.t` elements, and sometimes we want to call some ocaml code which returns a pure Ocaml element.

This does not give much solution, either you hide the javascript type into an abstract element (like dbuenzli did in [Brr](https://erratique.ch/software/brr/doc/Brr/index.html), for example [Jstr.t](https://erratique.ch/software/brr/doc/Jstr/index.html) is the equivalent of `js_string Js.t`), or you ensure that everything typechecks using another trick (but this is another discussion I do not want to be dragged into it 🙂 )

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 20, 2023, 11:55am UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/6 "2023-03-20T11:55:25Z")

</div>

Is the following an accurate mental model:

`'a Js.t` is nothing more than JS object that pretty promises to have some properties

What is the point of the pretty promise ? So that for JS FFI functions, instead of writing:

`js object -> js object -> js object` we can do something slightly more type safe, il.e.

`'a Js.t -> 'b Js.t -> 'c Js.t`

Is there any other use / utility of `'a Js.t` ?

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [March 20, 2023, 12:46pm UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/7 "2023-03-20T12:46:44Z")

</div>

> [@zeroexcuses](#):
>
> `'a Js.t` is nothing more than JS object that pretty promises to have some properties

That’s it. And the properties are encoded into an OCaml object type.

It’s a nice trick, but not very ergonomic in practice – like phantom types often are. You can read more on the reasons that lead me to `brr`’s approach in brr’s FFI manual [foreword](https://erratique.ch/software/brr/doc/ffi_manual#foreword).

---

<div class="post-metadata">

**Author:** ![zbaylin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zbaylin/32/4047_2.png) [@zbaylin](https://discuss.ocaml.org/u/zbaylin)\
**Post date:** [March 20, 2023, 1:31pm UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/8 "2023-03-20T13:31:44Z")

</div>

> [@zeroexcuses](#):
>
> 1. This table is outdated as of jsoo 5.1.0 right? Since we now have OCaml string == JS string

This is still true as of 5.1. The only difference is that now OCaml strings are encoded as UTF-8 strings in Javascript. The difference becomes clear when dealing with multi-byte codepoints:

 ![Screenshot 2023-03-20 at 9.28.34 AM](https://us1.discourse-cdn.com/flex020/uploads/ocaml/original/2X/5/575ac5c32ab8190a6500eb8f0935c562ebc3e7bd.png)

Javascript strings are UTF-16, so when the UTF-8 encoded string `0xF0 0x9F 0x98 0x8A` is dumped into JS, the result above is produced.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [March 20, 2023, 2:40pm UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/9 "2023-03-20T14:40:43Z")

</div>

> [@zbaylin](#):
>
> The only difference is that now OCaml strings are encoded as UTF-8 strings in Javascript.

That’s not exactly what’s happening :–)

An OCaml `string` is a sequence of arbitrary _bytes_. In the new representation, that sequence of bytes (_not_ necessarily UTF-8 valid) is encoded into a JavaScript UTF-16 string by using the unicode characters in the range U+0000 to U+00FF – a so called _JavaScript binary string_ (this used to be documented on MDN but it seems someone decided to kill the terminology, which is silly because it means there must be a broken link in `brr`’s doc and some JavaScript functions work with these strings e.g. `atob` and `bota`. Here’s the [archived page](https://web.archive.org/web/20220602070927/https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary) about them)

Since we are there, one nice orthogonal change that happened in js\_of\_ocaml 5 regarding strings is that you can now define JavaScript string literals directly from your UTF-8 encoded OCaml sources. That is in some static scenarios `js_of_ocaml` spots the conversion from a literal OCaml string interpreted as UTF-8 to a JavaScript (UTF-16) string and does it at compile time instead, resulting in a JavaScript string literal in the produced JavaScript (rather than, previously, a byte escaped UTF-8 sequence to be converted to UTF-16 at runtime)

So now, when you want a “literal” JavaScript string containing 🐪, you can write (in `brr`)

```
let ocaml = Jstr.v "🐫"

```

to get it without any conversion costs at runtime (more details in [this issue](https://github.com/ocsigen/js_of_ocaml/issues/1034)):

```sh
echo 'let ocaml = Jstr.v "🐫"' > test.ml                  
ocamlfind ocamlc -g -linkall -linkpkg -package brr test.ml
js_of_ocaml --linkall a.out -o test.js                    
grep '🐫' test.js                                         
[...](3821,[0,"🐫"],"Test");fb(0);r(3822,[0],"Std_exit");return}(globalThis));

```

.

---

<div class="post-metadata">

**Author:** ![zbaylin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zbaylin/32/4047_2.png) [@zbaylin](https://discuss.ocaml.org/u/zbaylin)\
**Post date:** [March 20, 2023, 4:39pm UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/10 "2023-03-20T16:39:07Z")

</div>

Ah, thanks @dbuenzli. I think what you’re saying is what I was trying to convey, but probably with poor wording. Thanks for the clarification

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 21, 2023, 2:06am UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/11 "2023-03-21T02:06:37Z")

</div>

@dbuenzli : I’m doing research on Brr. Can you tell me how Note/frp plays into this? Is it meant to be an alternative to react ?

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [March 21, 2023, 7:49am UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/12 "2023-03-21T07:49:29Z")

</div>

I suppose you meant react as in JavaScript (`Note` is an alternative to that [other react](https://erratique.ch/software/react), explanations about their relationship [here](https://github.com/dbuenzli/note#history)).

`Brr` just provides an ergonomic JavaScript FFI and bindings to established and forward looking browser APIs. The `Note` stuff you find in `Brr` is here by accident (used by private code of mine) and will be tweaked and moved away to an optional dependency of `Note` at some point.

Regarding whether Note/frp is an alternative to JavaScript react, I never understood what was so great about JavaScript react/elm architecture/whatever hype du moment so I can’t tell you but Note/frp is definitively a good way to program user interfaces in a sane and compositional way. In case you missed it there’s a [todomvc example](https://github.com/dbuenzli/brr/blob/master/test/todomvc.ml) here.

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 21, 2023, 9:38am UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/13 "2023-03-21T09:38:45Z")

</div>

@dbuenzli : Apologies if this comes off as pedantic.

I’m working through the test\_midi.ml example, and am looking at your usage of: [Result\_syntax (brr.Fut.Result\_syntax)](https://erratique.ch/software/brr/doc/Fut/Result_syntax/index.html)

Question: Why does Brr (a JS binding library) have its own Future/Result extension syntax? Intuitively, I’d expect there to be a standard approach to this in some foundational / core OCaml library. A typed JS/DOM/Web library seems a weird place to add this.

Therefore, I’m clearly not seeing something important – what is the driving motivation behind Fut/Result\_syntax ?

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 21, 2023, 9:39am UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/14 "2023-03-21T09:39:54Z")

</div>

Also, is this related to async JS ? By the keyword `Fut`, I’m expecting something to do with JS promises, and thus JS async, but I’m not seeing it in the test\_midi.ml example.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [March 21, 2023, 10:03am UTC](https://discuss.ocaml.org/t/bool-bool-js-t/11723/15 "2023-03-21T10:03:05Z")

</div>

> [@zeroexcuses](#):
>
> Why does Brr (a JS binding library) have its own Future/Result extension syntax?

Because a JavaScript binding library needs to deal with JavaScript promises and `Promise.resolve` can’t be typed in OCaml see [this comment](https://github.com/dbuenzli/brr/blob/13c6b7c49f2e76627f24b91bd94bc474cdcd77bb/src/fut.ml#L6-L26) in `fut.ml`.
