0

I have a string map like this:

let table = Belt.Map.String.fromArray([|("A", 1), ("B", 1), ("C", 3)|]);

When I compile this single line, BuckleScript JS output says that table is not a pure module. From my understanding of the Map, when you do a look up either the entry exists in which case it returns Some(value) or None, and should not have any side effects as far as I can tell. So, why is BS saying that it's not 'pure'?

glennsl
  • 28,186
  • 12
  • 57
  • 75
PKK
  • 753
  • 2
  • 7
  • 18
  • I wouldn't read too much into that. The OCaml type system has no effect-tracking (yet), so it's simply not possible to determine accurately whether side-effects may occur, except in the case of very simple self-contained modules. I wouldn't even be so sure that "pure" as used here has anything to do with side-effects. English isn't the BuckleScript author's first language, and that often shows. – glennsl Jan 18 '19 at 15:16
  • Hmm.. OK, but there doesn't seem to be a language issue in this case, the output says either "No side effects" or "Not a pure module", so the intent seems to be to flag side effects, which would be useful.. – PKK Jan 19 '19 at 02:30
  • 1
    It will be useful once JavaScript bundlers agree on a way to recognize pure modules and do more thorough optimizations on them. In this particular case, the 'not a pure module' message is probably caused by the internals of `Belt.Map` doing some low-level optimizations. – Yawar Jan 19 '19 at 18:01
  • I stand corrected then. but I'm still not so sure it's all that useful without proper effect-tracking. `Belt.Map` does use lots of imperative features to optimize it, but I don't think that's the real underlying cause. Even `List.length([])` isn't considered "pure" by BuckleScript, even when the obvious implementation is about as simple and pure as it can get. External modules simply don't carry that information, so therefore only very simple self-contained modules can accurately be determined to be pure, and hence its usefulness is very limited. – glennsl Jan 20 '19 at 00:26
  • Perhaps it's a small experiment and the seed of a bigger idea that's just been left in, or maybe an attempt to optimize for some micro-benchmark which Bob is quite fond of. – glennsl Jan 20 '19 at 00:30

0 Answers0