Skip to content

Commit

Permalink
templates are now a tagged type
Browse files Browse the repository at this point in the history
Defaults are no longer stored in the template; this allows defaults to be changed
without affecting cases where the default was explicitly set.
Also, listtem now includes unknown fields.
  • Loading branch information
akkartik committed Feb 3, 2012
1 parent f755555 commit d2a1f50
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
47 changes: 29 additions & 18 deletions lib/tem.arc
Expand Up @@ -14,13 +14,27 @@
(pair fields)))
(templates* ',name))))

(def inst (tem . args)
(let x (table)
(each (k v) (if (acons tem) tem (templates* tem))
(unless (no v) (= (x k) (v))))
(each (k v) (pair args)
(= (x k) v))
x))
; (tagged 'tem (tem-type fields nils))
(def inst (tem-type . args)
(annotate 'tem (list tem-type
(coerce pair.args 'table)
(memtable (map car (keep no:cadr pair.args))))))

(extend sref (tem v k) (isa tem 'tem)
(sref rep.tem.1 v k)
(if v
(wipe rep.tem.2.k)
(set rep.tem.2.k)))

(defcall tem (tem k)
(or rep.tem.1.k
(if (no rep.tem.2.k)
((alref (templates* rep.tem.0) k)))))

(defmethod iso(a b) tem
(and (isa a 'tem)
(isa b 'tem)
(iso rep.a rep.b)))

(def temload (tem file)
(w/infile i file (temread tem i)))
Expand All @@ -32,24 +46,21 @@
(writefile (temlist tem val) file))

(def temread (tem (o str (stdin)))
(let x (read str 'eof)
(if (~is 'eof x)
(listtem tem x))))
(let fields (read str 'eof)
(if (~is 'eof fields)
(listtem tem fields))))

(def temwrite (tem val (o o (stdout)))
(write (temlist tem val) o))

; like coerce, but requires a template and ignores unknown fields
(def listtem (tem raw)
(with (x (inst tem) fields (if (acons tem) tem (templates* tem)))
(each (k v) raw
(when (assoc k fields)
(= (x k) v)))
x))
; coerce alist to a specific template
; ignores unknown fields
(def listtem (tem fields)
(apply inst tem (apply + fields)))

; like tablist, but include nil fields
(def temlist (tem val)
(ret fields (coerce val 'cons)
(ret fields (coerce rep.val.1 'cons)
(each (k v) (if acons.tem
tem
templates*.tem)
Expand Down
25 changes: 21 additions & 4 deletions lib/tem.arc.t
Expand Up @@ -23,10 +23,6 @@
'((field1 nil))
(temlist 'foo (inst 'foo 'field1 nil)))

(test-iso "listtem ignores unknown fields"
(inst 'foo)
(listtem 'foo '((new-field 34))))
(test-iso "temlist and listtem are converses"
(inst 'foo 'field1 34)
(listtem 'foo (temlist 'foo (inst 'foo 'field1 34))))
Expand All @@ -37,3 +33,24 @@
(temwrite 'foo (inst 'foo 'field1 34) o)
(inside o))
(temread 'foo i)))

(test-iso "temread and temwrite are converses - 2"
(inst 'foo 'field1 nil)
(w/instring i (w/outstring o
(temwrite 'foo (inst 'foo 'field1 nil) o)
(inside o))
(temread 'foo i)))

(test-iso "nil in file overwrites default"
nil
(w/instring i (w/outstring o
(temwrite 'foo (inst 'foo 'field1 nil) o)
(inside o))
((temread 'foo i) 'field1)))
(test-iso "templates can distinguish explicit defaults from implicit defaults"
(obj field1 'default)
(let tem (inst 'foo)
(= tem!field1 34)
(= tem!field1 'default) ; explicit set
rep.tem.1))

1 comment on commit d2a1f50

@akkartik
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This broke fields with non-constant defaults; see http://arclanguage.org/item?id=17373. As of 2013-02-27 we no longer support the use case of modifying defaults.

Please sign in to comment.