Thread overview
Safer binary reading (or writing) code
Dec 12
Joel
Dec 12
Joel
Dec 13
cc
December 12

I've got this mixin thing, I think it's less typo-prone. I haven't been able to make it show the variable's name, though. Also, it should be optional whether it prints anything, (it's not hard for me to do that though).

// mixin(jread("width")); -> fread(&width, 1, width.sizeof, bfile);
auto jread(string var) => `fread(&`~var~`, 1, `~var~`.sizeof, bfile); writeln("read var=", `~var~`);`;
December 12

On Tuesday, 12 December 2023 at 09:43:39 UTC, Joel wrote:

>

I've got this mixin thing, I think it's less typo-prone. I haven't been able to make it show the variable's name, though. Also, it should be optional whether it prints anything, (it's not hard for me to do that though).

// mixin(jread("width")); -> fread(&width, 1, width.sizeof, bfile);
auto jread(string var) => `fread(&`~var~`, 1, `~var~`.sizeof, bfile); writeln("read var=", `~var~`);`;

O, got it:

auto jread(string var) => `fread(&`~var~`, 1, `~var~`.sizeof, bfile); writeln("read `~var~`=", `~var~`);`;
December 13

On Tuesday, 12 December 2023 at 09:43:39 UTC, Joel wrote:

>

I've got this mixin thing, I think it's less typo-prone. I haven't been able to make it show the variable's name, though. Also, it should be optional whether it prints anything, (it's not hard for me to do that though).

// mixin(jread("width")); -> fread(&width, 1, width.sizeof, bfile);
auto jread(string var) => `fread(&`~var~`, 1, `~var~`.sizeof, bfile); writeln("read var=", `~var~`);`;

When I need to insert a variable name numerous times into a bit of mixin code, I often use a replace pattern like this:

mixin(q{
	static struct _aa_$FIELD {
		KeyType!(typeof(field)) key;
		ValueType!(typeof(field)) val;
	}
	Array!(_aa_$FIELD) $FIELD;
}.replace("$FIELD", myVariableName));