Arc Forumnew | comments | leaders | submitlogin
Wart update: types, extensible '= and defgeneric (github.com)
1 point by akkartik 5381 days ago | 1 comment


2 points by akkartik 5381 days ago | link

= is just Common Lisp's setf, but I've setup setf to be aware of types and ssyntax. To make a new type assignable you configure how to coerce it to function=, and to make a new type subscriptable/callable you specify how to coerce to function. Here are the relevant bits from github:

  ; call.lisp
  (defmacro defcall(type arg &rest body)
    `(defcoerce ',type 'function
       (lambda(,arg)
         ,@body)))

  ; set.lisp
  (defmacro defset(type args &rest body)
    `(defcoerce ',type 'function=
      (lambda ,args
        ,@body)))


  ; list.wart
  (defcall cons l
    [elt l _])

  (defset cons(l index val)
    (setf (elt l index)
          val))

  ; hash.wart
  (defcall hash-table table
    [gethash _ table])

  (defset hash-table(table key value)
    (setf (gethash key table)
          value))

  wart> (call '(1 2 3) 1)
  2
  wart> (call (obj 1 2 3 4) 1)
  2
  wart> (ret a '(1 2 3) (= a.2 34))
  (1 2 34)
  wart> (ret a (obj 1 2 3 4) (= a.1 67))
  ; (obj 1 67 3 4)
I'm now trying to track down why nested ssyntax like hash.key1.key2 doesn't work, that should give you a sense of how alpha this is. But I'll figure it out, and when I do I'll write a test for it. Unit tests constitute 50% of the codebase.

-----