Bruce Korb <bk...@gnu.org> writes: > So because it might be the case that one reference might want to > see changes made via another reference then the whole concept is > trashed? "all or nothing"? Anyway, such a concept should be kept > very simple: functions that modify their argument make copies of > any input argument that is read only. Any other SCM's lying about > that refer to the unmodified object continue referring to that > same unmodified object. No mind reading required. > > (define a "hello") > (define b a) > (string-upcase! a) > b
In order to do as you suggest, we'd have to change `string-upcase!' from procedure to syntax. That's because `string-upcase!' gets a _copy_ of the pointer contained in `a', and is unable to change the pointer in `a'. This is fundamental to the semantics of Scheme. We cannot change it without breaking a _lot_ of code. If we changed every string mutation procedure to syntax, then you wouldn't be able to do things like this: (string-upcase! (proc arg ...)) (map string-upcase! list-of-strings) Also, if you wrote a procedure like this: (define (downcase-all-but-first! s) (string-downcase! s) (string-set! s 0 (char-upcase (string-ref s 0)))) it would work properly for mutable strings, but if you passed a read-only string, it would do nothing at all from the caller's point of view, because it would change the pointer in the local parameter s, but not the caller's pointer. These proposed semantics are bad because they don't compose well. > "it goes without saying (but I'll say it anyway)": > > (define a (string-copy "hello")) > (define b a) > (string-upcase! a) > b > > *does* yield "HELLO" and not "hello". Why the inconsistency? You are proceeding from the assumption that each variable contains its own string buffer, when in fact they contain pointers, and (define b a) copies only the pointer. In other words, the code above is like: char *a = string_copy ("hello"); char *b = a; string_upcase_x (a); return b; What you are asking for cannot be done without changing the fundamental semantics of Scheme at a very deep level. Mark