Js.log(list) output for lists with more than three elements


#1

This code:

let example = [10, 11, 12, 13, 14, 15];
Js.log(example);

produces this output:

[ 10, [ 11, [ 12, [Array] ] ] ]

Though technically correct, as the generated code is a JavaScript array, this seems highly confusing, since Reason arrays and lists are two very different things.

From what I understand, on previous versions of ReasonML, the output read Object instead of Array

Is the word Array something that can be changed to either List or ..., or would this break the universe?


#2

This is totally depending on your Javascript engine. Nothing to do with Bucklescript/ReasonML side.


#3

Aha. That explains everything. Thanks!


#4

In my experience logging Reason lists is not very useful because it is a nested structure and you can’t see beyond a few elements. So I tend to use a helper to convert lists into array before logging:

let logl = l => l |> Array.of_list |> Js.log;
logl([1,2,3]);

But if the values inside are not primitives - aka records or variants - then directly logging them doesn’t output any of the metadata (key names or constructors). So I end up writing a toJson encoder for every main module type which gets rendered nicely on the browser. It is a bit of manual work, but worth it for long-lived codebases.

BuckleScript has a debug mode (https://bucklescript.github.io/docs/en/better-data-structures-printing-debug-mode) that’ll retain the key names and print complex structures nicely, but last I checked it didn’t work well for variants with constructor arguments.