2011/2/8 Hongwei Xi <hwxi@cs.bu.edu>
On Tue, 8 Feb 2011, Guillaume Brunerie wrote:

>>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 string is non-linear; it can only be free by GC
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.

Thank you, but perhaps you mean that a strptr *must* be freed? Because I guess that the linear typing will not allow a memory leak here.

>>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
this:

fun rmdir_string ...
fun rmdir_strptr
fun rmdir_strbuf

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.

But you could use string_of_strbuf or string_of_strptr, why do you need to cast with such unsafe functions?


Guillaume