> From: Mark H Weaver <[email protected]>
> No, `define' does not copy an object, it merely makes a new reference to
> an existing object. This is also true in C for that matter, so this is
> behavior is quite mainstream. For example, the following program dies
> with SIGSEGV on most modern systems, including GNU/Linux:
>
> int
> main()
> {
> char *y = "hello";
> y[0] = 'a';
> return 0;
> }
True, but the following also is quite mainstream
int main()
{
char y[6] = "hello";
y[0] = 'a';
return 0;
}
C provides a way to create and initialize a mutable string.
> Scheme and Guile are the same as C in this respect. Earlier versions of
> Guile didn't make a copy of the string in this case either, but it
> lacked the mechanism to detect this error, and allowed you to modify the
> string literal in the program text itself, which is a _very_ bad idea.
It all depends on your mental model. Your saying that (define y "hello")
attaches "hello" to y, and since "hello" is a immutable, the string y
contains must be immutable. This is an argument based on purity, not
utility.
If you follow that logic, then Guile is left without any shorthand
to create and initialize a mutable string other than
(define y (substring "hello" 0))
or
(define y (string-copy "hello"))
Someone coming from any other language would be surpised to find that
the above is what you need to do to create an initialize a mutable string,
I think.
But 'define' just as easily can be considered a generic constructor
that is overloaded in a C++ sense, and when "hello" is a string, y is
assigned a copy-on-write version of the immutable string.
It was wrong to change this without deprecating it first.
Thanks,
Mike Gran