paulyoung / purescript-corefn

A library for working with the PureScript functional core.
Apache License 2.0
23 stars 7 forks source link

Add support for PSString #57

Open felixSchl opened 6 years ago

felixSchl commented 6 years ago

The PSString does not always serialize to a string, but might end up as an array of integers. The comment in the sources explain this in detail:

https://github.com/purescript/purescript/blob/e184fca6ffd04de45d4d8c4f6747230ae266c6d4/src/Language/PureScript/PSString.hs#L39-L54

I am not sure how to go about this. Create an PSString equivalent with two constructors?

paulyoung commented 6 years ago

@hdgarrood, @michaelficarra; any suggestions?

hdgarrood commented 6 years ago

The first approach I would consider would be to just use String; the reason we have PSString is because there's no direct equivalent to JS' String type in Haskell, but the String type is PureScript is of course the same as the String type in JS (and IIRC the approach we have taken so far for alternate backends is that we have said their String type also needs to be able to handle lone surrogates). You might find some more useful info in the relevant discussions on the compiler issue tracker; this all happened a little while ago and I don't remember it all that well.

michaelficarra commented 6 years ago

Yeah as @hdgarrood says, the PureScript String type is isomorphic to the Haskell PSString type, so you can just use that. Alternatively, a list of integers (optionally bounded between 0 and 0x10FFFF inclusive) is always sufficient for representing PureScript string data.

felixSchl commented 6 years ago

What does that mean in terms of JSON and recovering a JS string from it? I suppose the we'd need to implement this: https://github.com/purescript/purescript/blob/e184fca6ffd04de45d4d8c4f6747230ae266c6d4/src/Language/PureScript/PSString.hs#L145-L156?

hdgarrood commented 6 years ago

I don't think so; the strings library should make this easy. I think you can use

traverse Char.fromCharCode >>> map String.fromCharArray

where String is the name Data.String.CodeUnits has been imported under.