| akkartik's "Wart: File I/O for tables" (http://arclanguage.org/item?id=10677) reminded me of a question I've had for a while: what would be a good syntax for table literals? MzScheme uses "#hash(...)" with dotted pairs: arc> (obj a 1 b 2)
#hash((a . 1) (b . 2))
One possibility is that we could use Arc's terminology of "table" and drop the verboseness of the dotted pairs: arc> (obj a 1 b 2)
#table((a 1) (b 2))
arc> (#table((a 1) (b 2)) 'a)
1
In http://hacks.catdancer.ws/table-reader-writer.html, I used a syntax of curly braces, with no internal parentheses grouping keys and values: arc> (obj a 1 b 2)
{a 1 b 2}
arc> ({a 1 b 2} 'a)
1
I had been worried that in tables with a lot of items, it would be easy to lose track of which ones were keys and which ones were values. But in practice I've found that my keys and values usually look different enough that it hasn't been a problem for me.However, I've also noticed that I've never used the {...} table literal form myself when writing a program. I use it all the time when reading and writing table values in files, but when I'm creating a table value myself in a program, I'm always using something like (obj ...). In languages such as Perl or JavaScript, { ... } can be used as a template to construct table values: {"a": 5 - 4}
but in Lisp languages, programs and data have the same syntax, so with a literal syntax we'll get the literal expression, instead of the result of evaluating the expression: arc> {a (- 5 4)}
{a (- 5 4)}
we could add some kind of "unquote" mechanism to the literal table syntax, but frankly when I want to put the result of an expression evaluation into a table, I just use (obj ...): (obj a (- 5 4))
Since I never type the literal table syntax, I think it's a bad idea to tie up valuable syntax real estate by using { ... }, so I'm back to using something like #table(...).We could borrow the idea from (obj ...) that I used in my { ... } and dispense with the unnecessary internal parentheses grouping the keys and values. This would make #table(...) look like: arc> (obj a 1 b 2)
#table(a 1 b 2)
arc> (#table(a 1 b 2) 'a)
1
Thoughts? What do you think would be a good table literal syntax for Arc? |