awwx.ws

implicitreq0

Implicit request

http://awwx.ws/implicitreq0.arc:

(implicit req)

(mac idefop (name . body)
  (w/uniq request
    `(defop ,name ,request
       (w/req ,request ,@body))))

(mac idefopr (name . body)
  (w/uniq request
    `(defopr ,name ,request
       (w/req ,request ,@body))))

(extend arg args (is len.args 1)
  (arg req car.args))

(mac iaform (f . body)
  (w/uniq request
    `(tag (form method 'post action fnurl*)
       (fnid-field (fnid (fn (,request)
                           (prn)
                           (w/req ,request ,f))))
       ,@body)))

(def iflink (f)
  (string fnurl* "?fnid=" (fnid (fn (req) (w/req req (prn) (f))))))

(mac iw/link (expr . body)
  `(tag (a href (iflink (fn () ,expr)))
     ,@body))

(mac ionlink (text . body)
  `(w/link (do ,@body) (pr ,text)))

This hack makes req an implicit variable, so that it can be used without having to explicitly pass it around as an argument. For example, I can say (arg "foo") instead of (arg req "foo").

I’ve implemented implicit variants for a number of the srv definitions (the ones that I happen to be using in my code right now) by prefixing their names with “i”. So instead of

(defop hi req
  (pr "Hello " (arg req "name")))

I can use

(idefop hi
  (pr "Hello " (arg "name")))

This makes the Arc Challenge longer since we can no longer give different requests different names:

(idefop said
  (iaform (let v (arg "foo")
            (ionlink "click here" (pr "you said: " v)))
    (input "foo") 
    (submit)))

however in my own code I use the request object as a convenient place to stash all kinds of things related to responding to the current request: cookies to set, the user’s session, who the user is logged in as... so making the request object implicit is an overall win for me since I use it in many different places.

(Technically I could in fact make the Arc Challenge shorter by mixing the standard Arc forms with my implicit variants, but that’s getting into making programs shorter by abbreviations instead of actually reducing the number of conceptual nodes in the program).

Prerequisites

This hack depends on arc3.1, implicit2, and extend0.

Get this hack

Using the hackinator:

$ hack \
    ycombinator.com/arc/arc3.1.tar \
    awwx.ws/defarc0.patch \
    awwx.ws/defarc-ac0.patch \
    awwx.ws/extend0.arc \
    awwx.ws/scheme0.arc \
    awwx.ws/defvar1.patch \
    awwx.ws/defvar2.arc \
    awwx.ws/implicit2.arc \
    awwx.ws/implicitreq0.arc

License

Same as Arc.

Contact me

Twitter: awwx
Email: andrew.wilcox [at] gmail.com