primitive (byte) string literal with length?

Oleg Grenrus oleg.grenrus at
Wed Aug 25 16:05:58 UTC 2021

There are bytearray literal proposals [1,2]. My older proposal [2] idea
that the literal prim literal strings could generate also ByteArray# and
(# Int#, Addr# #), but as it was proposing to change how literal Haskell
Strings are compiled the proposal got stalled. The newew proposal [1] is
tagged as "needs revision". It doesn't include(# Int#, Addr# #), but
those are easy to get from ByteArray# which has negligible overhead. I
haven't followed the discussion so I'm not sure what syntax it actually
proposes (description and proposal text differ) and what are the needed

I'm cc-ing Andew, he knows better :)

- Oleg


On 25.8.2021 18.31, Viktor Dukhovni wrote:
> On Tue, Aug 24, 2021 at 09:03:30AM -0400, Viktor Dukhovni wrote:
> I originally wrote:
>>>> Is there any GHC syntax for constructing a primitive string literal
>>>> with a known (not hand coded) byte count?
>>>> With `"some bytes"#` I get just the `Addr#` pointer, but not the size.
>>>> If there's nothing available, would it be reasonable to introduce a new
>>>> syntax?
>>>> Perhaps:
>>>> 	"some bytes"## :: (# Addr#, Int# #)
> But neglected to mention that I knew about `cstringLength#`, but found
> it wanting, because it does not support octet-strings with embedded NUL
> characters:
>> Sadly, that does not work when the primitive octet string contains
>> internal NUL bytes.
>>     λ> :set -package ghc-prim
>>     λ> :set -XMagicHash
>>     λ> import GHC.CString
>>     λ> import GHC.Int
>>     λ>
>>     λ> I# (cstringLength# "foobar\xa0"#)
>>     7
>>     λ> I# (cstringLength# "foo\0bar\xa0"#)
>>     3
> If there isn't some other extant work-around, any feedback on my
> proposal of a new syntax for a primitive unboxed (address, length) pair:
>     "some bytes"## :: (# Addr#, Int# #)

More information about the ghc-devs mailing list