Arc Forumnew | comments | leaders | submitlogin
1 point by almkglor 6248 days ago | link | parent

The problem is the destructuring. In my proposed solution already we might have some pretty deep attachment layers:

  (def add-attachments (atts obj)
    (if (no atts)
      obj
      (add-attachments (cdr:cdr atts) (add-attachment (car atts) (car:cdr atts) obj))))
  (= foo
     (with (...)
       ...
       (add-attachments
         (list 'keys key-getter-function
               '=    setter-function
               'len  len:key-getter-function)
         reader-function)))
In such a case, annotate ends up being effectively a cons (with annotate==cons, type==car, rep==cdr). The structure then approaches an association list. O(n) lookups and all that.

When the road is clean, more people can explore where it leads.



3 points by nex3 6248 days ago | link

The thing is, I don't think annotate should be used as cons. The idea is that it attaches symbols which represent types - not that it bolts on new data. If you want data, you should annotate a cons or a table (or a cons with a table). Like, for example, if for some reason you wanted to create a point type with annotate, I think you wouldn't want to do

  (annotate x y)
but rather

  (annotate 'point (cons x y))
That's a silly example, because points are so simple, but for your attachment functions, I think adding to an actual list or table is a better solution than treating annotations as cons cells.

-----

1 point by almkglor 6248 days ago | link

Ah, the code example you gave threw me. I suppose what you really meant was something more like this:

  (def add-attachment (k v s)
    (if (isa s 'settable-fn)
        (do (= ((car (rep s)) k) v)
            s)
        (annotate 'settable-fn
            (cons (fill-table (table) (list k v)) s))))
re annotations as cons cells - well, that's already how they work, and apparently it's not a bug, as you mentioned.

-----

4 points by nex3 6248 days ago | link

Oh, I certainly don't mean that annotations aren't equivalent to cons cells. That's definitely true. I'm just saying that that doesn't mean we should use them like we use cons cells. I think their primary use should be the one your code example shows: to add a type to some other object.

-----