[Top][All Lists]

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

when to use strings, when to use symbols

From: Andy Wingo
Subject: when to use strings, when to use symbols
Date: Sat, 08 Jan 2011 18:25:32 -0800
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)

Hey all,

I have been going over the new web interface in Guile 2.0, making sure
that it's OK for long-term use, and got myself into a quandary about
when to use strings and when to use symbols in an interface.  I don't
pretend to have the right answer here, but this is the answer that I
have come to.

Let's say you have an association list.  It could be (("foo" . "bar")) or
((foo . "bar")) or ((foo bar)), or even (("foo" . bar)).  If you're
parsing this data from over the network, in textual form, which do you

The answer is this:

  * Use symbols when you want to match against a symbol literal in your
    source code.

  * Use strings otherwise.

  * Don't worry about symbol-interning attacks, where the remote side
    fills up your symbol table -- symbols are properly garbage-collected
    in Guile 2.0.

So in this case, the answer is ((foo . "bar")) if you would ever do
(assoc-ref alist 'foo) in code, and (("foo" . "bar")) otherwise.



ps. I very, very rarely hold Python up as an example of language design,
especially regarding strings, but I think that their approach to string
mutabililty and identity is preferable to the symbol vs string
distinction that Scheme has.

reply via email to

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