opMixin or mixin function templates with convenience operator?

mipri mipri at minimaltype.com
Fri Dec 13 15:24:32 UTC 2019


On Friday, 13 December 2019 at 14:52:27 UTC, Ola Fosheim Grøstad 
wrote:
> What is impolite is to try to shoot down the relevance of
> having AST macros by pointing to Forth and Lisp.

What I am trying to do is state that a bias towards specialized
features rather than generic ones is a very reasonable bias for
a language designer to have. That's it. If a metaprogramming
DIP came along I wouldn't object that "Lisp tried this and it
was bad".

> What are you
> trying to achieve by pointing to ancient languages that use
> cryptic identifiers for historic reasons?

I mentioned PARSE and REFILL from Forth, and reader macros from
Common Lisp. The age and identifiers of these languages has
nothing to do with the capabilities of those features. What
they represent is a kind of 'final DIP'. If any DIP can be
objected to with "this can be done in a library", then the
final DIP is the one that lets you do anything at all in a
library. If you *don't* have a bias towards specific solutions
then I think you'll eventually come up with a solution like
this, and I propose that it -- not "AST macros" -- is a bad
idea.

> AST macros are no worse than templates.

They really are, though. They're invariably much uglier and
harder to maintain than templates. I think that if you want to
have AST macros, you should certainly have a feature like
templates as both A) a stopgap, so that not everything has to
be done with AST macros, and B) a destination for AST macros.

It's a very different 'template', but consider this Nim:

   import strutils

   func maybeQuote(str: string): string =
     if {' ', ','} in str:
       '\'' & str & '\''
     else:
       str

   template `-->`(str: typed, field: untyped): untyped =
     str & ':' & maybeQuote($field)

   echo "hi" --> "hi"

Output: hi:hi

With a macro you could introduce a syntax

   echo --- hi

With the same output of "hi:hi"

How should you do that? Well, you can do it entirely in the
ugly macro system, or you can have 1-2 lines of code that just
emits `"hi" --> "hi"`, using the template. The macro becomes a
way of augmenting the template with a stringification power,
and the combination of template * macro is much easier to read
and understand than would be a macro generating the final Nim.

I imagine I'd come to the same conclusions about how best to
employ AST macros in D.

> And certainly a lot better than string mixins!



More information about the Digitalmars-d mailing list