5 ms·
Perl devs have been using the walrus expression for decades, but we just called it "assigning a variable with local scope in an expression". $ perl $a =
by peterwwillis 7y ago
Perl devs have been using the walrus expression for decades, but we just called it "assigning a variable with local scope in an expression".
$ perl
$a = "foo";
if ( my $a = "bar" ) { print "$a\n" }
print "$a\n"
bar
foo
- geofft 7y agoYup, C and many other languages also have it: if (ptr = strchr(address, ':')) port = atoi(ptr + 1);
- iso-8859-1 7y agoIt doesn't do scoping, so not really the same.
- gpderetta 7y agoit can in C++ (when specifying auto or a type). I thought it did in C as well, but apparently not.
- takeda 7y agoThe reason why assignment expressions initially were not allowed in Python and why they had to introduce "walrus" was because in languages that used single equal sign for assignment enable to easily make bugs by typing "=" instead of "==".
- deleted 7y ago[deleted]