problem-solving
problem-solving copied to clipboard
`constant` for many identifiers in single declaration
A constant declaration with multiple constants in a single statement should be orthogonal to variable declarations.
As of 6.d Raku can declare a single constant, but not many constants, in one statement. I was disappointed that it didn't mirror variable declarations.
my $w=11; # OK constant w=11; # OK my ($x,$y)=22,33; # OK constant (x,y)=22,33; # ===SORRY!=== Error while compiling: Missing initializer on constant declaration
Can Raku extend the constant declaration to allow many constants on the same line?
This is an interesting proposal. Possibly the issue right now is the relationship between constants and sigilless variables? I've tried to delve into recent comments on SO:
https://stackoverflow.com/questions/55210794/sigilless-variables-constants-bindings-when-to-choose-what
https://stackoverflow.com/questions/50399784/is-there-a-purpose-or-benefit-in-prohibiting-sigilless-variables-from-rebinding/50401989#50401989
https://stackoverflow.com/questions/55211168/useless-use-of-hash-composer-or-cannot-modify-an-immutable-hash/55212923#55212923
The significant difference is that:
my ($x,$y)=22,33; # OK
Is an assignment operation. The semantics are:
- Declare
$xand$yas if we'd writtenmy $x; my $y; - Form a list containing
$x, $y- noting that this is a list of (assignable) containers - Call the
STOREmethod on that, passing the list(22, 33)
The = in a constant initialization, however, implies binding. This does actually work out:
my ($x,$y) := 22,33;
Because the ($x, $y) is actually parsed as a signature, and only decays to a list of containers in the case that we are initializing it with assignment. Thus signature binding semantics apply.
We could, in principle, parse a signature after constant too. One would have to express the declaration as:
constant (\x, \y) = 22, 33;
Other than that, it achieves the goal without having to introduce any new syntactic or semantic concepts, and furthermore means one can enjoy other signature-related features (for example, initializing constants by destructuring the data on the right).
Interesting, good explanation, thanks! I hadn't considered binding vs assignment as how it relates to constants.
As a workaround right now this works, using the \-sigil to mimic a constant
my (\x, \y) = 7,8;
say x,y," rpm"; # 78 rpm
Since we appear to be clear as to what would need to be done (parsing a signature after constant), it feels that this issue should be closed here, and moved to a Rakudo issue requesting the implementation?
Yes; I suspect it will be rather easier to do in the RakuAST-based compiler, however, since I'm already planning to have standard destructures compiled there (rather than interpreted by the signature binder, as happens today).