Arc Forumnew | comments | leaders | submitlogin
Table defaults
3 points by skenney26 5650 days ago | 5 comments
Rather than specifying a default value for a table at lookup, it would be easier to set it when the table is created.

I'm not sure how best to implement it, but this is what I have so far (ac.scm):

  (define hash-table-default (ar-gensym))

  (xdef table (lambda args
                (let ((h (make-hash-table 'equal)))
                  (hash-table-put! h hash-table-default
                                     (if (pair? args) (car args) 'nil))
                  h)))
Each table is given an initial default value. ar-apply then looks for the value of a key or returns the default:

  ((hash-table? fn)
   (ar-nill (or (hash-table-get fn (car args) #f)
                (hash-table-get fn hash-table-default #f))))
Test:

  arc> (= h (table))
  #hash((gs1 . nil))
  arc> h!k
  nil
  arc> (= h (table 0))
  #hash((gs1 . 0))
  arc> h!k
  0


3 points by CatDancer 5650 days ago | link

oh, you're storing the default value inside the hash table itself. It took me a while to figure out what you were doing.

If you store the default value outside of the table then it won't show up when we print out the table, look at its keys, iterate over its keys and values, call tablist on it etc.

For example, you could store all the table defaults in its own hash table, using the tables as the keys and the default values as the values of the table:

  (define table-defaults (make-hash-table 'weak))
This creates a hash table which compares keys by their identity with eq? and doesn't prevent them from being garbage collected. (http://download.plt-scheme.org/doc/372/html/mzscheme/mzschem...)

-----

2 points by pg 5650 days ago | link

I thought about specifying the default when the table was created, but it seemed more flexible to do it at lookup, because you could choose a value for the default (e.g. a newly created object) that would let you distinguish between having no entry for some key and having an entry whose value was the default.

-----

3 points by Adlai 5650 days ago | link

What if a default specified at lookup overrode the default specified when the table was created?

My Arc knowledge is essentially #f at the moment, but this

  ((hash-table? fn)
   (ar-nill (or (hash-table-get fn (car args) #f)
                (hash-table-get fn hash-table-default #f))))
seems to be the code for looking up a key using the (some-table key) syntax (i.e. putting the table as the "function" in a form). Would it work to have an optional parameter, so that somebody could call (some-table key default) if they wanted to override the default? I'm thinking that this optional parameter would default to #f if unspecified, and would be passed to the first hash-table-get in the above code. This seems to enable overriding of a pre-specified default.

-----

1 point by skenney26 5650 days ago | link

I like your idea of having a default that can be overridden.

The code above is actually Scheme (which Arc is implemented in). Getting and setting table values in Arc is much less clunky.

-----

1 point by Adlai 5650 days ago | link

Yeah, I can tell it's Scheme because of #f used for false, instead of NIL.

If it's possible to pass an optional parameter to an Arc-style hash-table lookup (one where you use the table name as the function), than that would also allow overriding a default to NIL, because the 'or in that Scheme code would return NIL rather than evaluating the hash-table-get for the original default.

-----