Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.95k stars 554 forks source link

:= operator for creating my variables #18881

Closed philiprbrenan closed 3 years ago

philiprbrenan commented 3 years ago

Please reduce:

 my $a = 1;
 my @a = (1..2);
 my %a = (1..2);

down to the following as the reduction in boiler plate makes it much easier to read:

$a := 1;         # syntax error near "$a :"
@a := (1..2);    # syntax error near "@a :"
%a := (1..2);    # syntax error near "%a :"

Thanks!

richardleach commented 3 years ago

It seems to me like this request would use up some available syntax just to provide an alias.

Additionally, it might well make it harder for newcomers to reason about scopes and assignment operators. e.g.

Also, I don't know how easy it would be to spot typos (or if the interpreter would be able to warn you) where you meant to type:

state $foo = 1;
<---more code here--->
$foo = 7;

But accidentally typed:

state $foo = 1;
<---more code here--->
$foo := 7;
oodler577 commented 3 years ago

Unless this is to match what many are used to in bourne shell, then I'd avoid := (or :- for that matter) altogether. It's also meaningful to make/gmake. Maybe we can use ===. I think that one's still waiting to be properly abused.

https://stackoverflow.com/questions/4437573/bash-assign-default-value

leonerd commented 3 years ago

I'd also downvote this as an idea. I have vague thoughts about using the := syntax for signature-like list assignment, as per my "Perl in 2025" FOSDEM talk. Namely, the idea that a list assignment written using := would behave identically to signature unpacking, in that it would complain about too few or too many items, apply defaults, etc...

my ($x, $y, $z = 10) := @values;

would get upset if @values < 2 or @values > 3, and would assign $z to $values[2] if it exists, or default to 10 if not.

But even in that talk I explained that maybe := is far too contentious, subtle, or hard to intuit from merely reading the name, and so maybe a keyword would be better instead.

Leont commented 3 years ago

As Larry once said, everyone wants the colon. It's still true today.

happy-barney commented 3 years ago

You will still need my keyword to declare variable without initialization expression. There are also other keywords: our, local, state

Leont commented 3 years ago

Given the reactions, I think we can close this