Arc Forumnew | comments | leaders | submitlogin
2 points by nlavine 5924 days ago | link | parent

Good idea! I have one question: can we generalize this?

We already have objects like tables which act one way in functional position and another way in argument position. Can we say that you're suggesting adding a third way = setter target position?

If so, can we extend annotate in a general manner to let you define objects which have all three aspects? (And in the future, maybe, we could let people define their own, but I'm not sure yet how this would work, or what else exactly you would use it for, so we should wait on that.) Here's your example, modified a bit:

  (= foo
      (with (var1 1 var2 2 var3 3 var4 4)
        (annotate
          (fn (v)                 ; functional position aspect
           (case v
             'var1 var1
             'var2 var2
             'var3 var3
             'var4 var4))
          (fn (newv v)            ; setter target aspect
            (case v
              'var1 (= var1 newv) ; (I think these would have to be symbols)
              'var2 (= var2 newv)
              'var3 (= var3 newv)
              'var4 (= var4 newv))
          (fn ()                  ; object aspect
            (list var1 var2 var3 var4)))))


1 point by almkglor 5923 days ago | link

Hmm. Maybe a better generalization is to take advantage of this code in ac.scm:

   (define (ar-tagged? x)
     (and (vector? x) (eq? (vector-ref x 0) 'tagged)))
annotate creates a vector with 3 entries:

  (annotate 'foo (fn () nil))
  => #3(tagged,foo,<procedure>)
Let's then create a basic function which retains the type in the second entry of the vector, but adds a fourth entry containing a hash.

  (attach '= (fn (v) (settercode v))
    (fn () (readercode)))
  => #4(tagged,fn,<procedure>,#hash((= . <procedure>)))
The benefit here is we could add other aspects of the object - for example, getting a length, or determining the keys so we can traverse the object.

Hmm. Might actually implement this. Am planning a persistent table, such that assignments to persistent-table!key will automatically update a file on the server (which, incidentally, can also encapsulate a database instead)

-----

1 point by almkglor 5924 days ago | link

minor nitpick: 'case already has implicit (quote ...) on each of the objects in the case position.

-----

1 point by nlavine 5924 days ago | link

Oh, right. Good call. (feels abashed)

-----