opMixin or mixin function templates with convenience operator?

Zoadian no at no.no
Thu Dec 12 11:48:47 UTC 2019


On Thursday, 12 December 2019 at 10:33:21 UTC, aliak wrote:
> So the interpolated dip thread got me thinking, and wondering 
> if any time there's been a proposal for an opMixin? It can 
> possibly be the hash tag symbol? And opMixin does what mixin 
> does, and mixes the code in the spot.
>
> struct interpolator {
>   void opMixin!(string)() {
>   }
> }
>
> // globally declared interpolator
> interpolator i;
>
> --- code.d
>
> import interpolation;
>
> string str = i#"an ${interpolated} string";
>
> Advantages over template mixins include that this has a return 
> type so it can be used to create things out of the current 
> context and give you something back.
>
> auto html = json#q{ // compile error if invalid json
>   "hello": "${variable}",
>   "another: "${a + b}",
> };
>
> Another one is function mixin templates? So maybe that can be 
> done with eponymous mixin templates?
>
> mixin template i(string str) {
>     string i() {
>         return str;
>     }
> }
>
> string s = i#"someting";
>
> If it can't be done eponymously then how about:
>
> mixin string s(string str)() {
>   // parse str
>   return mixin(stuff);
> }
>
> You get the hygiene with # as I believe it's not used anywhere? 
> And this enables string interpolation libraries that have the 
> syntax that any or all of the dips that are being propose have.
>
> - ali

this is similar to my idea. for reference:
https://forum.dlang.org/post/qqrblbqygugpkkqgttxp@forum.dlang.org

I'm all in favor of improving D meta programming to the point we 
can express stuff like string interpolation in library code 
because it might allow for other new cool stuff we were not able 
to do before.


More information about the Digitalmars-d mailing list