c-mera icon indicating copy to clipboard operation
c-mera copied to clipboard

Saner template syntax

Open kiselgra opened this issue 9 years ago • 2 comments

Similar to #32 it would be cool to have

(decl ((#:std:tuple<int *, #:std:string> foo)) ...)

or maybe

(decl ((#:std:tuple<(int) (#:std:string) (const char *)> foo)) ...)

kiselgra avatar Dec 10 '16 02:12 kiselgra

I think this is still an open issue, but wanted to provide the following suggestion.

If you use certain templates a lot, the instantiate-syntax is a nuisance and it might be reasonable to define a few macros to provide more readable code, e.g.

(defmacro vector (&rest type)
  `(instantiate #:std:vector (,@type)))

This simply splices together the correct vector instantiation (without support for allocators, for the sake of terseness):

(decl (((instantiate vector (unsigned int)) foo)
       ((vector unsigned int) bar)))

Both declarations produce the same output.

If you feel that the angle-brackets are important for readability you could use a similar macro:

(defmacro vector< (&rest type)
  `(instantiate #:std:vector (,@(butlast type))))

In this case, the last element is (blindly) supposed to be >. All versions listed below emit the same declaration:

(decl (((instantiate vector (unsigned int)) foo)
       ((vector unsigned int) bar)
       ((vector< unsigned int >) baz)))

@nihofm Saw a lot of instantiate in your last commits to ert. :)

kiselgra avatar Nov 16 '17 11:11 kiselgra

Not sure how /sane/ this is, but we could define another instantiation macro using something similar to split-sequence:

(lisp
  (defun split-seq (seq sym)
    (loop for x on seq
          if (not (member (car x) (list sym '< '>))) collect (car x) into ret
          if (eql (car x) sym) return `(,ret ,@(split-seq (rest x) sym))
          finally (return (list ret)))))

and then just break the list of types on some symbol:

(defmacro inst (type &rest args)
  `(instantiate ,type ,@(split-seq args '!)))

(decl (((inst tuple < unsigned int ! float ! const float ! char >) test)))

Sadly, we cannot use any of the quotation symbols nor . which makes this more awkward... I'm not saying we should have this in c-mera, but might be a work-around for users really put off by instantiate ;)

kiselgra avatar Nov 16 '17 12:11 kiselgra