Closed philiprbrenan closed 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.
my $a;
or my ($a, @a, %a);
, so code might have to have a mixture of my
and :=
, and this requires the reader to remember that both mean the same lexical scoping.local
, our
, scope
- still all come before the variable name and don't have corresponding alternatives. People might find reading our $foo = 1; $bar := 2;
harder than our $foo = 1; my $bar = 2;
+=
or .=
, things that change or use the current value. 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;
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
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.
As Larry once said, everyone wants the colon. It's still true today.
You will still need my
keyword to declare variable without initialization expression.
There are also other keywords: our
, local
, state
Given the reactions, I think we can close this
Please reduce:
down to the following as the reduction in boiler plate makes it much easier to read:
Thanks!