[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

helper macro construct-from-string for srfi-108

This page is part of the web mail archives of SRFI 108 from before July 7th, 2015. The new archives for SRFI 108 contain all messages, not just those from before July 7th, 2015.

[Sorry for the long quiet - among other issues I just
sold my house, which got a bit crazy at times.]

In the current draft (http://per.bothner.com/tmp/srfi-108/srfi-108.html)
section "Resolving to constructor" I suggest a common use-case would
be to implement
  &cname[pre-exp ...]{abc&[infix-exp1]def&[infix-exp2]...xyz}
  (cname-maker pre-exp ... &{abc&[infix-exp1]def&[infix-exp2]...xyz})

I.e. map &cname to a "constructor function" cname-maker that
takes a string build from the non-initial arguments.  I figure this
handles a common use-case in a flexible way, and in a way that
builds on SRFI-109.  I've implemented a macro
'constructor-from-string' to make such mapping easier.

The idea is that you could implement $construct$:cname thus:

(define-syntax $construct$:cname
  (syntax-rules ()
    ((_ . args) (construct-from-string cname-maker . args))))

This assumes you also a function cname-maker.

&cname{abc&(+ 3 4)z} ==> (cname-maker "abc7z")
&cname[id: "n7"]{&(+ 3 4)abc} ==> (cname-maker id: "n7" "7abc")

So my question is:
(1) Is this useful enough to include in the specification?
(I'm obviously leaning towards "yes" here.)
(2) Any suggestions for a better name than construct-from-string?
That name suggests a "constructor function", but it's really
a macro to simplify writing constructor bindings, so it's not
a good name.
(3) Other tweaks to the API beside the name?

FWIW here is the implementation. It assumes initial arguments,
if any, are ended by the symbol $>>$.

(define-syntax construct-from-string
  (syntax-rules ()
    ((_ fun . args) (%construct-from-string-builder fun () . args))))

(define-syntax %construct-from-string-builder
  (syntax-rules ($<<$ $>>$)
    ((%construct-from-string-builder fun (seen ...) $<<$ . rest)
     (fun ($string$ seen ... $<<$ . rest)))
    ((%construct-from-string-builder fun (seen ...) $>>$ . rest)
     (fun seen ... ($string$ . rest)))
    ((%construct-from-string-builder fun (seen ...) x . rest)
     (%construct-from-string-builder fun (seen ... x) . rest))
    ((%construct-from-string-builder fun (seen ...))
     (fun seen ...))))
	--Per Bothner
per@xxxxxxxxxxx   http://per.bothner.com/