You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
localeCompare can return different output depending on locale. Example borrowed from MDN:
console.log("ä".localeCompare("z","de"));// a negative value: in German, ä sorts before zconsole.log("ä".localeCompare("z","sv"));// a positive value: in Swedish, ä sorts after z
The FFI for Data.String.Common.localeCompare uses the system default locale, so Data.String.Common.localeCompare "ä" "z" could be LT or GT depending on if it's run in Germany or Sweden, for example.
It seems unlikely (maybe even impossible? I'm not sure) for a system's locale to change over the lifetime of a program, so maybe that is enough to consider localeCompare observationally pure?
I'm mostly just curious about the intuition for what makes FFI code "effect"-ful, as I have a library of bindings for the Intl object and I was planning on adding to it a localeCompare that accepted the Locale argument as well.
I confirmed with the Intl authors that a JS program may observe the system locale change over the course of its execution, so localeCompare should indeed be considered impure.
I could make a PR to update the type here (along with the docs and tests), but looking at the rest of the library, this would be the first use of Effect outside of test code. I'm wondering if there's another discussion to be had around whether this function belongs in the library?
Tracking down when localeCompare was added, I was surprised to find it's been here since the beginning—a decade ago!
Well it's still useful when passed a locale explicitly. Maybe just require the locale parameter? Or is there maybe a more appropriate module for locale-sensitive operations to live in?
What's the type of the locale parameter? If it's String then it can throw
"a".localeCompare("b","zzzz")// throws a RangeError
Even if it were a well-typed Locale or Array Locale you'd have to account for the runtime not supporting localeCompare for that locale and falling back to the system locale.
Activity
garyb commentedon Jan 21, 2024
I agree, this does seem like it should be in
Effectsince it's based on the environment it's being run in.michaelficarra commentedon Jan 23, 2024
I confirmed with the
Intlauthors that a JS program may observe the system locale change over the course of its execution, solocaleCompareshould indeed be considered impure.pete-murphy commentedon Jan 25, 2024
I could make a PR to update the type here (along with the docs and tests), but looking at the rest of the library, this would be the first use of
Effectoutside of test code. I'm wondering if there's another discussion to be had around whether this function belongs in the library?Tracking down when
localeComparewas added, I was surprised to find it's been here since the beginning—a decade ago!michaelficarra commentedon Jan 25, 2024
Well it's still useful when passed a locale explicitly. Maybe just require the locale parameter? Or is there maybe a more appropriate module for locale-sensitive operations to live in?
pete-murphy commentedon Jan 25, 2024
I've been working on bindings for the locale-sensitive operations specified in ECMA 402 here: https://github.com/pete-murphy/purescript-js-intl/tree/main/src/JS/LocaleSensitive. From what I remember of last time I looked, I couldn't find any other references to any of those functions on Pursuit aside from this library's
localeCompare.What's the type of the locale parameter? If it's
Stringthen it can throwEven if it were a well-typed
LocaleorArray Localeyou'd have to account for the runtime not supportinglocaleComparefor that locale and falling back to the system locale.michaelficarra commentedon Jan 25, 2024
That's a good point. Then I support dropping it or moving it to another more appropriate place.
garyb commentedon Jan 25, 2024
I think it seems reasonable to drop it here 👍