Expanding text for comparison purposes... but not?

I have some code that looks roughly like this:

Before printing the name of a person (called P):
	if not expanding text for comparison purposes:
		now last person referenced is P;	

But it seems that sometimes last person referenced is getting set even when the name of P is not printed to the screen. Since I know there is nowhere else in my code where last person referenced is set, it must be the case that inform is expanding text for purposes other than comparison or printing. Or am I missing something?

expanding text for comparison purposes is true if we’re doing a text comparison, and false otherwise. It’s not the opposite of “outputing text”. When Inform performs the text expansion for the substituted form of a text, it doesn’t know or care whether it’s actually outputting that text.

j is initially 0.

yak is a thing.

before printing the name of yak when not expanding text for comparison purposes, increment j.

to puts (sv - a sayable value): say sv; say line break.

when play begins:
  puts yak;
  puts j; [ 1 ]
  if "[yak]" is "z", say "gosh.";
  puts j; [ still 1 ]
  let x be "[yak]";
  puts j; [ still 1 ]
  let y be the substituted form of x;
  puts j; [ now 2 ]

“yak” only gets printed once, but j ends up at 2. Odds are good you’ve got someplace you’re not thinking of where you’re expanding “[p]” where p is a person, but not outputting it.

1 Like

Yeah, the Standard Rules suggest that “expanding text for comparison purposes” means it’s not being printed to the screen, but in fact say__comp (the flag that it checks) is only set in TEXT_TY_Compare, the routine for comparing two texts against each other—not in TEXT_TY_Transmute, the routine that prints the text into a buffer to expand all the substitutions. Meaning if you ever manually do “the substituted form of”, that doesn’t set the flag.

Which is usually what you want. But you can get around this:

Include (-
[ SubstituteForComparison to txt ;
    @push say__comp;
    say__comp = 1;
    TEXT_TY_SubstitutedForm(to, txt);
    @pull say__comp;
    return to;
To decide what text is the substituted form of (T - text) for comparison purposes:
    (- SubstituteForComparison({-new:text}, {-by-reference:T}) -).

This is based on the code for “substituted form of” in Basic Inform, but it also sets the say__comp flag.


Beautiful, that fixed it! Thanks!

Aha! I had assumed, without being motivated enough to dig through the code to confirm it, that using ‘expanding text for comparison purposes’ doesn’t work during the activity ‘for printing the name of something’ (as highlighted in WI §5.7. Text with random alternatives) because when checking for a leading vowel to choose between ‘a’ and ‘an’ for the indefinite article the name was expanded to an internal buffer without invoking the text comparison machinery. Thank you for confirming that, and even more for the fix!