Arc Forumnew | comments | leaders | submitlogin
Indexed data structure support?
2 points by tjw 6403 days ago | 3 comments
If one wanted to implemented some indexed data structure (e.g. defstruct), wouldn't it be in the spirit of Arc to allow using such data structures in the functional position?

Example:

   (struct node name val)
   (let n (make-node)
      (= (node-name n) 'foo)
      (prn (node-name n)) ; -> 'FOO
      ;; This won't work
      (prn (n 'name))     ; -> Should also be 'FOO
      ;; This neither
      (= (n 'val) 1))
   	   
The Tcl language has a 'unknown' command that can be redefined and is called whenever the interpreter encounters something in the functional position it does not know how to handle. If Arc had something similar, one could handle the case of the node struct above:

    (let orig unknown
    	 (def unknown (expr)
	      (if (and (alist expr) (isa (car expr) 'node))
	      	  (if (is (cadr expr) 'name) (node-name (car expr))
		      (is (cadr expr) 'val)  (node-val  (car expr))
		      	  	      	     (err "bad index"))
		  (orig expr))))
Of course, this would be have to be done in the macro expansion of the (struct node ...) ...

To allow using = on a place specified this way, would there have to be an 'unknown=' procedure?

Such a general mechanism like 'unknown' could of course be (mis)used for other purposes, too ...



1 point by jimbokun 6402 days ago | link

Yep. I would also like a way to "override" = in a setf kind of way.

-----

3 points by nex3 6401 days ago | link

defset does just that.

-----

1 point by jimbokun 6401 days ago | link

Thanks for the tip!

-----