problem-solving icon indicating copy to clipboard operation
problem-solving copied to clipboard

`constant` for many identifiers in single declaration

Open fecundf opened this issue 4 years ago • 5 comments

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?

fecundf avatar Jan 02 '21 16:01 fecundf

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

jubilatious1 avatar Jan 06 '21 16:01 jubilatious1

The significant difference is that:

my ($x,$y)=22,33; # OK

Is an assignment operation. The semantics are:

  1. Declare $x and $y as if we'd written my $x; my $y;
  2. Form a list containing $x, $y - noting that this is a list of (assignable) containers
  3. Call the STORE method 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).

jnthn avatar Feb 12 '21 14:02 jnthn

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

fecundf avatar Feb 12 '21 16:02 fecundf

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?

lizmat avatar Jun 07 '21 11:06 lizmat

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).

jnthn avatar Jun 07 '21 12:06 jnthn