-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added support for default values in variable declarations.
Example: var (x, y=755, z=777) = (666, 655); say x; # prints: 666 say y; # prints: 655 say z; # prints: 777 Also, this includes the support for slurpy variables: var (a, *b) = (1,2,3,4); say a; # prints: 1 say b; # prints: [2,3,4] var *arr = (1,2,3); say arr; # prints: [1,2,3] var :hash = (a => 1, b => 2); say hash; # prints: Hash.new(a => 1, b => 2) As a side-effect, all variables are now declared at the top of the functions. This improves the performance inside loops, since no reinitialization is done.
- Loading branch information
trizen
committed
Oct 22, 2015
1 parent
96fe494
commit 5952210
Showing
4 changed files
with
109 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
package Sidef::Variable::Init { | ||
|
||
sub new { | ||
my (undef, @vars) = @_; | ||
bless {vars => \@vars}, __PACKAGE__; | ||
my (undef, %opt) = @_; | ||
bless \%opt, __PACKAGE__; | ||
} | ||
|
||
}; | ||
} | ||
|
||
1; |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/ruby | ||
|
||
# Test for declaration of variables with default values | ||
|
||
var (x, y=755, z=777) = (666, 655); | ||
|
||
assert_eq(x, 666); | ||
assert_eq(y, 655); | ||
assert_eq(z, 777); | ||
|
||
var (a, *b) = (1,2,3,4); | ||
assert_eq(a, 1); | ||
assert_eq(b, [2,3,4]); | ||
|
||
var *arr = (21,42,84); | ||
assert_eq(arr, [21, 42, 84]); | ||
|
||
var :hash = (a => 21, b => 42); | ||
assert_eq(hash, Hash.new(a => 21, b => 42)); | ||
|
||
assert_eq([var(p, q) = (9,10)], [9, 10]); | ||
assert_eq(p, 9); | ||
assert_eq(q, 10); | ||
|
||
assert_eq([var(s=7, t=3)], [7, 3]); | ||
assert_eq(s, 7); | ||
assert_eq(t, 3); | ||
|
||
assert_eq([var x = 42], [42]); | ||
assert_eq(x, 42); | ||
|
||
say "** Test passed!"; |