2011/2/8 Hongwei Xi <email@example.com>
On Tue, 8 Feb 2011, Guillaume Brunerie wrote:a string is non-linear; it can only be free by GC
>>But I have a few questions, because I don't really understand how to use
>>strings in ATS.
>>1) Could you explain the differences between 'string', 'strbuf', 'strptr' ?
>>(and 'free_gc_v', etc.)
a strbuf is like a flat array of bytes, where the last one is null.
strptr is a linear string, which can be manually freed.
>>In the example of
>>mkdtemp you gave, you are using a function from prelude/SATS/unsafe.sats
>>which does not seems very good (and I don't want to use such functions). Why
>>not having a type which says that the return value is either the null
>>pointer or the same pointer as the argument?
I did it in the fixed version. However, the unsafe cast was used for
a different purpose. Say that you want to call rmdir, which is given
the following type:
fun rmdir (path: string): int(*error code*)
Now suppose that you have a linear string or strbuf as your path.
You cannot pass it to rmdir directly. Of course, rmdir can be given
a type that accepts linear string in the first place but then you can no
longer call rmdir on a constant string. In the past, I did something like
fun rmdir_string ...
overload rmdir with rmdir_string
overload rmdir with rmdir_strptr
overload rmdir with rmdir_strbuf
This style was so awkward that I now prefer casting over it.