Last active
May 29, 2023 23:54
-
-
Save uzluisf/b3539dbf95ad6b1e158313f4646e6c05 to your computer and use it in GitHub Desktop.
Revisions
-
uzluisf revised this gist
May 29, 2023 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -93,8 +93,7 @@ say f; # [4, 5] Here, [the circumfix operator `[]` creates an `Array`](https://redd.it/sd6ls3), which is mutable by design and thus allows for assignments. With assignment sigilled variables create containers by default, thus you can update a variable's value through subsequent assignment. ```raku my $b = 42; @@ -131,4 +130,6 @@ Assigning to a scalar variable `$` means the content is itemized (i.e., a single foo = 99; say foo; # [99] ``` From lizmat, "Yes, because in at least all current language versions of Raku, `[]` is a circumfix operator creating an `Array`. And `Array`s are mutable by design." * [vrurg's clarification about sigilled variables being alias as much as sigilless variable, and that containers are created with assignment](https://redd.it/sd6ls3), instead of always being there. From vrurg: "Any symbol in Raku is an alias. Sigilled ones are no exception." From theangryepicbanana: "I just meant "alias" as in "it directly references the rhs", as opposed to a sigilled variable which creates a container [with assignment]". -
uzluisf revised this gist
May 29, 2023 . 1 changed file with 18 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ # Sigilless and Sigilled Variables in Raku There are several differences between sigilless and sigilled variables, which are already documented in the [Raku docs](https://docs.raku.org/). This is simply my attempt at consolidating them in a single place. ## Sigil @@ -80,7 +82,7 @@ my \a = 42; a = 84; # Error: Cannot modify an immutable Int (42) ``` If you bind a mutable structure to the sigilless variable, you can assign to the variable once it's been bound. ```raku my \f = [1, 2]; @@ -89,6 +91,9 @@ f = [4, 5]; say f; # [4, 5] ``` Here, [the circumfix operator `[]` creates an `Array`](https://redd.it/sd6ls3), which is mutable by design and thus allows for assignments. Sigilled variables have a container by default, thus you can update a variable's value through assignment. ```raku @@ -115,4 +120,15 @@ for $item { .say } # (1 2 3) ``` Assigning to a scalar variable `$` means the content is itemized (i.e., a single thing), shown by `$item` which when looped produces a single item, i.e., the list `(1, 2, 3)` itself. In contrast, the list in `list` isn't itemized and the `for` loops over its elements. ## References * [Lizmat on why you can assign to a `constant` sigilless variable declared to a mutable structure](https://redd.it/sd6ls3), namely ```raku my constant \foo = [42]; say foo; # [42] foo = 99; say foo; # [99] ``` From lizmat, "Yes, because in at least all current language versions of Raku, `[]` is a circumfix operator creating an `Array`. And `Array`s are mutable by design." -
uzluisf created this gist
May 29, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,118 @@ There are several differences between sigilless and sigilled variables, which are already documented in the [Raku docs](https://docs.raku.org/). This is simply my attempt at consolidating them in a single place. ## Sigil At the surface, a sigil or lack thereof is the most apparent difference between sigilless and sigilled variables. As their names imply, **sigilless variable** don't have sigils while **sigilled variables** do. ```raku my \degrees = pi / 180; my $angle = 90; ``` Sigilless variables are declared using the `\` prefix, which is dropped when using the variables. For sigilled variables, the sigil is part of the variable's name and thus must be used when both declaring a variable and using it. ```raku say degrees; say $angle; ``` ## Binding Sigilless variables are bound by default. You can bind sigilless variables with both the binding operator `:=` and the assignment operator `=`; both operators have the same effect. ```raku my \a = 42; my \b := 42; ``` In order to bind to a sigilled variable, you must use the binding operator `:=`. ```raku my $c := 42; ``` Unlike sigilless variables, you can always re-bind sigilled variables. ```raku my $c := 42; $c := 45; # Ok! ``` To prevent re-binding to a sigilled variable, you can declare it as a `constant`. ```raku my constant $d := 42; $d := 45; # Cannot bind to '$d' because it was bound in a signature and variables # bound in signatures cannot be rebound unless they were declared with the # 'is rw' or 'is copy' traits ``` ## Container A more striking difference between sigilless and sigilled variables is the fact sigilless variables do not have associated containers. ```raku my \a = 42; my $b = 42; say a.VAR.^name; # 42 say $b.VAR.^name; # Scalar ``` If the value bound to the sigilless variable is containerized, then its container will be the variable's container. ```raku my \c = [1, 2]; my @d = [1, 2]; say c.VAR.^name; # Array say @d.VAR.^name; # Array ``` ## Assignment Whether or not you can assign to a sigilless variable ultimately depends on what's bound to it. If it's an immutable structure (i.e., no container), as shown here with an `Int`, then you cannot assign to it once it's been declared. ```raku my \a = 42; a = 84; # Error: Cannot modify an immutable Int (42) ``` If you bind a container to the sigilless variable, you can assign to the variable once it's been bound. ```raku my \f = [1, 2]; say f; # [1, 2] f = [4, 5]; say f; # [4, 5] ``` Sigilled variables have a container by default, thus you can update a variable's value through assignment. ```raku my $b = 42; $b = 21; ``` ## Context Unlike sigilled variables, sigilless variables don't enforce context. ```raku my \list = (1, 2, 3); for list { .say } # OUTPUT: # 1 # 2 # 3 my $item = (1, 2, 3); for $item { .say } # OUTPUT: # (1 2 3) ``` Assigning to a scalar variable `$` means the content is itemized (i.e., a single thing), shown by `$item` which when looped produces a single item, i.e., the list `(1, 2, 3)` itself. In contrast, the list in `list` isn't itemized and the `for` loops over its elements.