Unsafe aspects of ByteString

Iavor Diatchki iavor.diatchki at gmail.com
Sun Jan 28 12:58:39 EST 2007


Hello,
The "packCString" function (and other similar functions) in the
ByteString library break referential transperancy, which is one of the
big selling points of Haskell (and its libraries).   Here is an
example:

main = do x <- newCString "Hello"
          let s   = packCString x
              h1  = B.head s
          print s
          -- print h1
          poke x (toEnum 97)
          print s
          let h2 = B.head s
          print h1
          print h2
Output:
"Hello"
"aello"
97
97

This is already confusing because the "pure" value 's' has magically
changed.   Also notice that the evaluation order of the program
affects the output.  If we include the commented out statement (which
forces h1 to be evaluated earlier) the output becomes:
"Hello"
72
"aello"
72
97

I think that because of this we should either remove these functions,
or at least follow the convention of other libraries and give them
"unsafe" names.

-Iavor


More information about the Libraries mailing list