Where are we with thoughts on string interpolation and mixins?
aliak
something at something.com
Tue Nov 20 11:06:05 UTC 2018
On Tuesday, 20 November 2018 at 10:09:40 UTC, Zoadian wrote:
> On Tuesday, 20 November 2018 at 09:19:41 UTC, aliak wrote:
>> Hello,
>>
>> I do a lot of work with mixins. But, it's usually very hard to
>> read, and therefore reason about, and therefor review, and
>> therefor maintain. So I'm wondering what people's thoughts are
>> on how we can improve the experience of using mixins.
>>
>> [...]
>
> While I do agree that string interp would be nice, I think it
> could be done in library code. Let me show you how I currently
> do something like it:
>
> ```
> private enum LIQUID_ASGRAM_DEFINE_API(string PRE, T, TC, TI) = `
> struct %PRE%_s;
> alias %PRE% = %PRE%_s*;
> %PRE% %PRE%_create(uint nfft);
> void %PRE%_destroy(%PRE% q);
> void %PRE%_reset(%PRE% q);
> void %PRE%_set_scale(%PRE% q, float ref_lvl, float div);
> void %PRE%_set_display(%PRE% q, const(char)* ascii);
> void %PRE%_push(%PRE% q, %TI% x);
> void %PRE%_write(%PRE% q, %TI%* x, uint n);
> void %PRE%_execute(%PRE% q, char* ascii, float * peakval,
> float * peakfreq);
> void %PRE%_print(%PRE% q);
> `.replace("%PRE%", PRE).replace("%T%",
> T.stringof).replace("%TC%", TC.stringof).replace("%TI%",
> TI.stringof);
>
> mixin(LIQUID_ASGRAM_DEFINE_API!("asgramcf", float,
> liquid_float_complex, liquid_float_complex));
>
> mixin(LIQUID_ASGRAM_DEFINE_API!("asgramf", float,
> liquid_float_complex, float));
> ```
You almost prove some of the same points I'm trying to make :p
"replace("%T%", T.stringof).replace("%TC%", TC.stringof)"
Are not needed as %T% and %TC% are not in that string.
And probably all the places you call LIQUID_ASGRAM_DEFINE_API
have extra arguments that negatively impact any kind of code
reviews. And this would happen constantly. Code evolves, so
people will change LIQUID_ASGRAM_DEFINE_API and review it, and
debug it.
But on a side note, I do think that in some cases a .format()
would be a lot more readable than string interop, especially
where you are reusing one variable a lot of times. Like in your
example:
private enum LIQUID_ASGRAM_DEFINE_API(string PRE, T, TC, TI) = `
struct %1_s;
alias %1 = %1_s*;
%1 %1_create(uint nfft);
void %1_destroy(%1 q);
void %1_reset(%1 q);
void %1_set_scale(%1 q, float ref_lvl, float div);
void %1_set_display(%1 q, const(char)* ascii);
void %1_push(%1 q, %2 x);
void %1_write(%1 q, %2* x, uint n);
void %1_execute(%1 q, char* ascii, float * peakval, float *
peakfreq);
void %1_print(%1 q);
`.format(PRE, TI.stringof);
Though, as the number of placeholders increase, so does your
mental burden.
Cheer,
- Ali
More information about the Digitalmars-d
mailing list