7 ms·
I once wrote a programming language that had a localised 'this' called 'it' exactly for this pattern. if (something_exists) { do_something_to(it) } It
by sambeau 13y ago
I once wrote a programming language that had a localised 'this' called 'it' exactly for this pattern.
if (something_exists) {
do_something_to(it)
}
It was really useful (and readable).
I believe Perl has something similar but it was less readable.
- draegtun 13y agore: Perl - You're probably thinking of $_, which most Perl programmers call it. However this only gets topicalised in bare for & while but not if. So you could do... for (something_exists) { do_something_to($_); } ... as long as something_exists doesn't return a list :) More common approach I've seen in Perl (and other languages where variable declarations return its value) is this: if (my $it = something_exists) { do_something_to($it); }