Skip to content

Commit

Permalink
- Added the ClassInit.alias() method to create aliases to methods.
Browse files Browse the repository at this point in the history
- Changed the implicit block-call method to "bcall".
- Implicit block-call method is now user as implicit ".each" for various objects.

Example:
	"abc" { |c|
	    say c;   # prints each character ("a", "b", "c")
	};

It's equivalent with:
	"abc".each { |c|
	   say c;
	};

This works with arrays, strings, numbers, range, multi-arrays and hashes
  • Loading branch information
trizen committed Sep 22, 2015
1 parent 086aebf commit 3eea5a1
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 10 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ scripts/merge_sort.sf
scripts/merge_sort_2.sf
scripts/metaprogramming_2.sf
scripts/metaprogramming_method_definition.sf
scripts/method_aliases.sf
scripts/method_expressions.sf
scripts/Module/Fact.sm
scripts/Module/Fib.sm
Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2154,7 +2154,7 @@ package Sidef::Parser {

push @{$struct{$self->{class}}[-1]{call}},
{
method => 'call',
method => 'bcall',
(%{$arg} ? (arg => [$arg]) : ())
};

Expand Down
4 changes: 3 additions & 1 deletion lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ package Sidef::Types::Array::Array {

*for = \&each;
*foreach = \&each;
*bcall = \&each;

sub each_index {
my ($self, $code) = @_;
Expand Down Expand Up @@ -574,7 +575,8 @@ package Sidef::Types::Array::Array {
my ($self, $code) = @_;
$self->new(
map {
map { $_->get_value } @{$code->run($_->get_value)}
map { $_->get_value }
@{$code->run($_->get_value)}
} @{$self}
);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Sidef/Types/Array/MultiArray.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package Sidef::Types::Array::MultiArray {
bless \@array, __PACKAGE__;
}

*call = \&new;

sub get_value {
my ($self) = @_;
[
Expand Down Expand Up @@ -60,6 +62,7 @@ package Sidef::Types::Array::MultiArray {
$self;
}

*bcall = \&each;
*iter = \&each;
*iterate = \&each;

Expand Down
2 changes: 2 additions & 0 deletions lib/Sidef/Types/Array/Pair.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ package Sidef::Types::Array::Pair {
bless [map { Sidef::Variable::Variable->new(name => '', type => 'var', value => $_) } ($item1, $item2)], __PACKAGE__;
}

*call = \&new;

sub get_value {
my ($self) = @_;

Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Types/Array/RangeNumber.pm
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ package Sidef::Types::Array::RangeNumber {
$self;
}

*call = \&each;
*bcall = \&each;

sub map {
my ($self, $code) = @_;
Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Types/Array/RangeString.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ package Sidef::Types::Array::RangeString {
$self;
}

*call = \&each;
*bcall = \&each;

our $AUTOLOAD;
sub DESTROY { }
Expand Down
2 changes: 2 additions & 0 deletions lib/Sidef/Types/Block/Code.pm
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ package Sidef::Types::Block::Code {
$result;
}

*bcall = \&call;

sub if {
my ($self, $bool) = @_;

Expand Down
1 change: 1 addition & 0 deletions lib/Sidef/Types/Hash/Hash.pm
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ package Sidef::Types::Hash::Hash {
Sidef::Types::Array::Array->new(Sidef::Types::String::String->new($key), $value->get_value);
}

*bcall = \&each;
*each_pair = \&each;

sub sort_by {
Expand Down
2 changes: 2 additions & 0 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ package Sidef::Types::Number::Number {
$obj->repeat($self);
}

*bcall = \×

sub to_bin {
my ($self) = @_;
state $x = require Math::BigInt;
Expand Down
1 change: 1 addition & 0 deletions lib/Sidef/Types/String/String.pm
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ package Sidef::Types::String::String {
$self;
}

*bcall = \&each;
*each_char = \&each;

sub lines {
Expand Down
12 changes: 10 additions & 2 deletions lib/Sidef/Variable/ClassInit.pm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ package Sidef::Variable::ClassInit {
$self;
}

sub alias {
my ($self, $name, $method) = @_;
ref($method) eq 'Sidef::Types::Block::Code'
? ($self->{__METHODS__}{$name} = $method)
: ($self->{__METHODS__}{$name} = $self->{__METHODS__}{$method});
}

sub respond_to {
my ($self, $name) = @_;
Sidef::Types::Bool::Bool->new(exists $self->{__METHODS__}{$name});
Expand Down Expand Up @@ -175,8 +182,9 @@ package Sidef::Variable::ClassInit {
$class;
}

*call = \&init;
*new = \&init;
*call = \&init;
*bcall = \&init;
*new = \&init;

{
no strict 'refs';
Expand Down
48 changes: 44 additions & 4 deletions scripts/block_implicit_call.sf
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class my_if {
self;
};

# Alias "my_elsif" to "if"
__CLASS__.def_method(:my_elsif, new);
# Alias "my_elsif" to "my_if"
my_if.alias(:my_elsif, new);

method my_else(callback) {
!did && callback();
Expand All @@ -56,7 +56,7 @@ class my_if {
}

#
## Test elsif (alias for 'if')
## Test elsif (alias for 'my_if')
#
my_if (false) {
die "my_if error";
Expand Down Expand Up @@ -96,7 +96,47 @@ var obj = Test();

var y = 0;
obj.hello {
say "Test passed!";
y = 42;
};
assert_eq(y, 42);

#
## Class init with block call
#

class BlockTest(block) {
method brun {
block();
}
}

var j = 0;
var bt = BlockTest {
j++;
};
bt.brun;
assert_eq(j, 1);

#
## Block-call as implicit ".each" method
#

var z = 0;

4 { |i|
print i; # prints: 1,2,3,4
z++;
};

"abc" { |c|
print c; # prints each character of the string
z++;
};

['x', 'y'] { |item|
print item; # prints each item of the array
z++;
};

say "";
assert_eq(z, 4+3+2);
20 changes: 20 additions & 0 deletions scripts/method_aliases.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/ruby

#
## Example of method aliases
#

class Hi {
method hi {
say "Hello!";
};

Hi.alias("hello", hi); # `hi` refers directly to the method
Hi.alias("hey", "hello"); # `hello` refers to the method by name
}

var obj = Hi();

obj.hi;
obj.hello;
obj.hey;

0 comments on commit 3eea5a1

Please sign in to comment.