reasonrescript

Comparing String in Rescript


After browsing through Rescript's API, it seems like there is no function that compares 2 strings that returns a boolean. The best option is localeCompare but it behaves somewhat different from the JS's ==. Why does localeCompare return a float instead of an integer?


Solution

  • You can compare strings using == in rescript as well. Alternatively there is a String.equal as well if you need a function restricted to strings specifically, The "native" (non-Js) standard library modules such as String unfortunately seems to be left out of the rescript documentation entirely.

    localeComapre probably returns a float because it might be possible for it to return non-integer numbers. JavaScript unfortunately has no integer type, which makes it hard to know if it could return floats even if it seems obvious that it shouldn't. I've seen several of this kind of bugs myself in various bindings.