AccelerationNet / access

A common lisp library to unify access to common dictionary-like data-structures
Other
86 stars 12 forks source link

Feature Request: Provide a default value #21

Open phmarek opened 10 months ago

phmarek commented 10 months ago

Currently it's not possible to run

(incf (accesses o (1 :type array)) 15)

because the initial value is NIL, which can't be incremented.

How about allowing this here?

(incf (accesses o (1 :type array :default 0)) 15)
vindarel commented 10 months ago

Hi, wdyt of this?

(incf (or (accesses …) 
           0))
phmarek commented 10 months ago

Well, to be honest, I don't like it.

(let ((x (make-hash-table)))
  (incf (or (gethash 1 x) 0)))

doesn't work either (at least in SBCL), and it reads like (incf 0) which hurts my eyes. Sorry.

The point is that the read-form and the write-form of access:accesses need to be different, right?

phoe commented 10 months ago

If we follow the gethash example, in (incf (gethash 1 x 0)) the 0 default value is evaluated and possibly discarded if the hash table already has a value. I assume that modifying accesses with a default would need to work the same way in order to maintain the semantics of "the place should look the same no matter if we read or write from it".