[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Guile: What's wrong with this?

From: Mark H Weaver
Subject: Re: Guile: What's wrong with this?
Date: Wed, 04 Jan 2012 05:03:13 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.92 (gnu/linux)

Bruce Korb <address@hidden> writes:
> 2.  it is completely, utterly wrong to mutilate the
>     Guile library into such a contortion that it
>     interprets this:
>         (define y "hello")
>     to be a request to create an immutable string anyway.
>     It very, very plainly says, "make 'y' and fill it with
>     the string "hello".  Making it read only is crazy.

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:

    char *y = "hello";
    y[0] = 'a';
    return 0;

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.

For example, look at what Guile 1.8 does:

  guile> (let loop ((i 0))
           (define y "hello")
           (display y)
           (string-set! y i #\a)
           (loop (1+ i)))
  <then an error>

So you see, even in Guile 1.8, (define y "hello") didn't do what you
thought it did.  It didn't fill y with the string "hello".  You were
actually changing the program text itself, and that was a serious

I'm sincerely sorry that you got yourself into this mess, but I don't
see any good way out of it.  To fix it as you suggest would be like
suggesting that C should change the semantics of char *y = "hello" to
automaticallly do a strcpy because some existing programs were in the
habit of modifying the string constants of the program text.  That way
lies madness.

If you want to make a copy of a string constant from the program text as
a starting point for mutating the string, then you need to explicitly
copy it, just like in C.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]