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
{{ message }}
Make System.OsString.Posix.fromBytes and Systen.OsString.Windows.encodeFS total #6
It's not uncommon to work on a single-platform application or an area, which utilises only one flavour of OsString on both platforms. Recent work on tar is a prominent example, where PosixString is used as an internal representation even on Windows.
IMO it would be better to break the symmetry and provide
The lack of total conversions is the biggest friction point in my experience so far; if we can alleviate it at least in some scenarios, it's worth doing.
How would we migrate or deprecate? This is invasive and will require another major bump for two packages, as well as patches for directory at least, potentially with CPP.
It's probably no secret that I am no friend of IsString class: it has bugs and unclear semantics. So I'd rather avoid it, otherwise people will get hopes that OsString may get it.
Instead, GHC SC has to come up with a version of OverloadedStrings that is not broken.
It's probably no secret that I am no friend of IsString class: it has bugs and unclear semantics. So I'd rather avoid it, otherwise people will get hopes that OsString may get it.
Would IsString OsString be about as unsafe as IsString Text currently is or am I missing some massive usage caveat?
It's probably no secret that I am no friend of IsString class: it has bugs and unclear semantics. So I'd rather avoid it, otherwise people will get hopes that OsString may get it.
Would IsString OsString be about as unsafe as IsString Text currently is or am I missing some massive usage caveat?
There are two caveats:
OsString is not guaranteed to be unicode (unlike Text). So the IsString instance conveys a false sense of "this type is unicode-ish" (although the osstr quasi-quoter encodes as UTF-8/16, but it's more explicit, easier to document and can reject surrogates)
OsString is not guaranteed to be unicode (unlike Text).
But encoding itself is still deterministic on a given platform, so it is possible to make a total conversion function that encodes surrogates into either other surrogates or replacement characters, right?
OsString is not guaranteed to be unicode (unlike Text).
But encoding itself is still deterministic on a given platform, so it is possible to make a total conversion function that encodes surrogates into either other surrogates or replacement characters, right?
I'm not sure what definition of "total" you are proposing, but encode . decode == id does not hold.
The point I'm leading to is that IsString, while being a runtime-evaluated codec typeclass (bad in three different ways, I fully agree), is overwhelmingly only used for hardcoding strings. So in spirit the encode . decode == id rule only matters in regards to encoding valid Unicode points, handling invalid ones may as well be considered undefined behavior (though for performance reasons it should probably encode invalid points into other invalid points as it sees convenient).
The current lack of an IsString OsString instance means that instead of a pleasant "honk" I have to input either [osstr|honk|] or unsafeEncodeUtf "honk", and it gets even stupider with lists. There is no upside to this, other than perhaps confusing newcomers, who most probably don't even know what UTF surrogates are.
So ideally in a proper API I would expect:
An IsString instance with documentation stating usafe limits and that manual fromString use is never needed;
unsafeEncodeUtf in a clearly unsafe module (perhaps System.OsString.Unsafe) and matching the behavior of fromString.
(optional) encodeUtf/decodeUtf should return either Maybes or meaningful Eithers. The current exception format I assume is text cargo cult; a simple "character offset at which the error occurred" format would be far more useful.
the quasiquoter is safe, clearly documented and can even be made more explicit if need be (e.g. by renaming to osstrUnicode or exposing it through a .Unicode module)
we can also provide ascii and other quasi quoter variants => this approach is clearly superior
IsString is a misdesigned broken class that the community should stop using
OverloadedStrings as an extension most certainly could be fixed or redesigned... I have no problem with string literals! Maybe it should be called OverloadedUnicodeStrings.
the fact that users have to look for how to construct OsString is a good thing and means people are starting to become aware of the intricacies that surround strings and especially filepaths
I have written two large blog posts about Haskell filepath and Haskell strings:
The whole point of AFPP/OsPath/OsString was to be more correct than String (yes, in those pesky little corner cases too). Providing an IsString instance is a blow to that vision. People can still use String and FilePath if they don't care that much about correctness or write orphan instances how they see fit.
Since ByteString instance of IsString is also problematic, we already have incentive of fixing OverloadedStrings. I'm not on GHC SC, but I can surely help promote and support such attempts.
I know all the points you stated and I agree with all of them (except the "clearly superior" thing, else I wouldn't be writing this), but the community at large, including me, will only stop using IsString when better IsString arrives. Knowing how slow and painful GHC updates are, we're not going to see anything like that before year 2030. Until that day, every user of this library will be forced to bear with the nine-character-wide Template-Haskell-ridden quasiquoter overhead that doesn't seem to do anything and isn't even highlit properly in the editor.
Until that day, every user of this library will be forced to bear with the nine-character-wide Template-Haskell-ridden quasiquoter overhead
I'm not sure I understand what the argument here is. That the identifier is too long to type?
That's a rather poor argument in my book. There are ways to configure your editor with snippets and macros to make it easier. It may even be possible to write an HLS plugin to aid with that.
The real caveats of quasiquoters are:
slows down compilation time
some tooling might get confused (e.g. buggy formatters)
Also, you can just define an orphan IsString instance and then deal with the fallout.
This library was not written to be the most convenient String type. It was written to be the most correct one wrt OS API.
And reading all this, I'm considering to make it more prominent that the quasi quoter is UTF-8/16.
If we reach a point where people just randomly throw OsString literals around without understanding that these might not always be correct (e.g. bc the encoding of filepaths is actually not UTF), then we didn't gain much. Equality checks might suddenly fail, because there's no getFileSystemEncoding involved anymore (which is used for FilePath for instance, but it's also just an approximation).
There are ways to configure your editor with snippets and macros to make it easier.
I don't need to do that for any other part of the language and this library does not seem different enough to justify this. People who don't want to use IsString can simply choose not to use OverloadedStrings, which is opt-in.
e.g. bc the encoding of filepaths is actually not UTF
How about encoding as in truncating characters to bytes then? This is consistent with how ByteStrings already behave, matches what most people care about (hardcoding ASCII strings) and predictably fails for anyone who chooses to manually encode characters beyond ASCII.
I don't need to do that for any other part of the language and this library does not seem different enough to justify this. People who don't want to use IsString can simply choose not to use OverloadedStrings, which is opt-in.
I don't want to provide broken API.
How about encoding as in truncating characters to bytes then? This is consistent with how ByteStrings already behave, matches what most people care about (hardcoding ASCII strings) and predictably fails for anyone who chooses to manually encode characters beyond ASCII.
Are you suggesting the IsString instance should truncate like ByteString? Absolutely not. That is even worse, as we have seen evidence here: haskell/bytestring#140
I'm also not sure my example came across the way I wanted. Say, you have filepaths encoded via Shift JIS and now you create a literal of an expected filepath to search for in a directory. The bytes will never match. When dealing with String, you don't have that issue, because you're not comparing bytes, but already decoded Unicode Code Points. getFileSystemEncoding is the trick that the base library uses at the outer FFI layer to turn CString into FilePath. So it happen to work, if your filepaths match the encoding that getFileSystemEncoding (locale lookup) returns. That is a gamble too (e.g. when dealing with network drives or whatnot).
This is why os-string has a rich interface to deal with encodings:
encodeWith taking TextEncoding parameters for both platforms
base providing utilites to build your own TextEncoding via GHC.IO.Encoding
And it also provides the same base trick via encodeFS.
In some cases you might even want to construct a specific byte sequence as a literal, instead of going through String.
All these strategies have trade offs. There is no one right way. UTF-8 is not the one right way either, even if people want to believe that. If we did believe that, then this package would be useless.
Yes, I am suggesting exactly that, as it would be as consistently broken as every other IsString instance and its convenience and readability would far outweigh said brokenness (this is a trade-off like any other).
Say, you have filepaths encoded via Shift JIS and now you create a literal of an expected filepath to search for in a directory. The bytes will never match.
Yes, and with this approach non-ASCII hardcoding will almost never match locally either, consistently failing all relevant tests.
Activity
hasufell commentedon Dec 12, 2023
This would be a breaking change.
hasufell commentedon Dec 12, 2023
How would we migrate or deprecate? This is invasive and will require another major bump for two packages, as well as patches for
directoryat least, potentially with CPP.Bodigrim commentedon Dec 13, 2023
I'm not attached to names. Maybe just add two new functions:
and, say,
The latter function suggests that there could exist a well-behaved
instance IsString WindowsString...hasufell commentedon Dec 15, 2023
It's probably no secret that I am no friend of
IsStringclass: it has bugs and unclear semantics. So I'd rather avoid it, otherwise people will get hopes thatOsStringmay get it.Instead, GHC SC has to come up with a version of
OverloadedStringsthat is not broken.Bodigrim commentedon Dec 15, 2023
Fair enough. I'm not wed to any particular names, anything with pure type signature will do.
Add IO-free variants of some functions
hasufell commentedon Dec 26, 2023
#11
BurningWitness commentedon May 17, 2024
Would
IsString OsStringbe about as unsafe asIsString Textcurrently is or am I missing some massive usage caveat?hasufell commentedon May 17, 2024
There are two caveats:
OsStringis not guaranteed to be unicode (unlike Text). So theIsStringinstance conveys a false sense of "this type is unicode-ish" (although theosstrquasi-quoter encodes as UTF-8/16, but it's more explicit, easier to document and can reject surrogates)BurningWitness commentedon May 17, 2024
But encoding itself is still deterministic on a given platform, so it is possible to make a total conversion function that encodes surrogates into either other surrogates or replacement characters, right?
hasufell commentedon May 17, 2024
I'm not sure what definition of "total" you are proposing, but
encode . decode == iddoes not hold.What does "other surrogates" mean? Proper UTF-8 does not allow surrogate range. If you're proposing PEP-383, then the unicode consortium has already voiced its displeasure: https://unicode.org/L2/L2009/09236-pep383-problems.html
BurningWitness commentedon May 17, 2024
The point I'm leading to is that
IsString, while being a runtime-evaluated codec typeclass (bad in three different ways, I fully agree), is overwhelmingly only used for hardcoding strings. So in spirit theencode . decode == idrule only matters in regards to encoding valid Unicode points, handling invalid ones may as well be considered undefined behavior (though for performance reasons it should probably encode invalid points into other invalid points as it sees convenient).The current lack of an
IsString OsStringinstance means that instead of a pleasant"honk"I have to input either[osstr|honk|]orunsafeEncodeUtf "honk", and it gets even stupider with lists. There is no upside to this, other than perhaps confusing newcomers, who most probably don't even know what UTF surrogates are.So ideally in a proper API I would expect:
An
IsStringinstance with documentation stating usafe limits and that manualfromStringuse is never needed;unsafeEncodeUtfin a clearly unsafe module (perhapsSystem.OsString.Unsafe) and matching the behavior offromString.(optional)
encodeUtf/decodeUtfshould return eitherMaybes or meaningfulEithers. The current exception format I assume istextcargo cult; a simple "character offset at which the error occurred" format would be far more useful.hasufell commentedon May 17, 2024
I have to re-iterate:
osstrUnicodeor exposing it through a.Unicodemodule)IsStringis a misdesigned broken class that the community should stop usingOverloadedStringsas an extension most certainly could be fixed or redesigned... I have no problem with string literals! Maybe it should be calledOverloadedUnicodeStrings.I have written two large blog posts about Haskell filepath and Haskell strings:
The whole point of AFPP/OsPath/OsString was to be more correct than String (yes, in those pesky little corner cases too). Providing an IsString instance is a blow to that vision. People can still use String and FilePath if they don't care that much about correctness or write orphan instances how they see fit.
Since ByteString instance of IsString is also problematic, we already have incentive of fixing OverloadedStrings. I'm not on GHC SC, but I can surely help promote and support such attempts.
BurningWitness commentedon May 17, 2024
I know all the points you stated and I agree with all of them (except the "clearly superior" thing, else I wouldn't be writing this), but the community at large, including me, will only stop using
IsStringwhen betterIsStringarrives. Knowing how slow and painful GHC updates are, we're not going to see anything like that before year 2030. Until that day, every user of this library will be forced to bear with the nine-character-wide Template-Haskell-ridden quasiquoter overhead that doesn't seem to do anything and isn't even highlit properly in the editor.hasufell commentedon May 18, 2024
I'm not sure I understand what the argument here is. That the identifier is too long to type?
That's a rather poor argument in my book. There are ways to configure your editor with snippets and macros to make it easier. It may even be possible to write an HLS plugin to aid with that.
The real caveats of quasiquoters are:
Also, you can just define an orphan IsString instance and then deal with the fallout.
This library was not written to be the most convenient String type. It was written to be the most correct one wrt OS API.
And reading all this, I'm considering to make it more prominent that the quasi quoter is UTF-8/16.
If we reach a point where people just randomly throw OsString literals around without understanding that these might not always be correct (e.g. bc the encoding of filepaths is actually not UTF), then we didn't gain much. Equality checks might suddenly fail, because there's no
getFileSystemEncodinginvolved anymore (which is used for FilePath for instance, but it's also just an approximation).BurningWitness commentedon May 18, 2024
I don't need to do that for any other part of the language and this library does not seem different enough to justify this. People who don't want to use
IsStringcan simply choose not to useOverloadedStrings, which is opt-in.How about encoding as in truncating characters to bytes then? This is consistent with how
ByteStrings already behave, matches what most people care about (hardcoding ASCII strings) and predictably fails for anyone who chooses to manually encode characters beyond ASCII.hasufell commentedon May 18, 2024
I don't want to provide broken API.
Are you suggesting the
IsStringinstance should truncate like ByteString? Absolutely not. That is even worse, as we have seen evidence here: haskell/bytestring#140I'm also not sure my example came across the way I wanted. Say, you have filepaths encoded via Shift JIS and now you create a literal of an expected filepath to search for in a directory. The bytes will never match. When dealing with
String, you don't have that issue, because you're not comparing bytes, but already decoded Unicode Code Points.getFileSystemEncodingis the trick that the base library uses at the outer FFI layer to turnCStringintoFilePath. So it happen to work, if your filepaths match the encoding thatgetFileSystemEncoding(locale lookup) returns. That is a gamble too (e.g. when dealing with network drives or whatnot).This is why
os-stringhas a rich interface to deal with encodings:TextEncodingparameters for both platformsAnd it also provides the same base trick via encodeFS.
In some cases you might even want to construct a specific byte sequence as a literal, instead of going through String.
All these strategies have trade offs. There is no one right way. UTF-8 is not the one right way either, even if people want to believe that. If we did believe that, then this package would be useless.
BurningWitness commentedon May 18, 2024
Yes, I am suggesting exactly that, as it would be as consistently broken as every other
IsStringinstance and its convenience and readability would far outweigh said brokenness (this is a trade-off like any other).Yes, and with this approach non-ASCII hardcoding will almost never match locally either, consistently failing all relevant tests.