Skip to content

Commit

Permalink
- Added the method method for user-defined classes which returns a …
Browse files Browse the repository at this point in the history
…LazyMethod object, or nil

- Renamed user-defined class method `has_method` into `respond_to`
  • Loading branch information
trizen committed Jul 7, 2015
1 parent 6122dd9 commit dbf29e5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 19 deletions.
2 changes: 0 additions & 2 deletions lib/Sidef.pm
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ package Sidef {
Sidef::Types::Bool::Bool->new($self->can($method));
}

*respondTo = \&respond_to;

sub is_a {
my ($self, $obj) = @_;
Sidef::Types::Bool::Bool->new(ref($self) eq ref($obj));
Expand Down
9 changes: 8 additions & 1 deletion lib/Sidef/Variable/Class.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ package Sidef::Variable::Class {
Sidef::Types::Hash::Hash->new(%methods);
}

sub method {
my ($self, $name) = @_;
exists($self->{method}{$name})
? Sidef::Variable::LazyMethod->new(obj => $self, method => $self->{method}{$name})
: ();
}

sub def_method {
my ($self, $name, $block) = @_;
$self->{method}{$name} = $block;
}

sub has_method {
sub respond_to {
my ($self, $name) = @_;
Sidef::Types::Bool::Bool->new(exists $self->{method}{$name});
}
Expand Down
20 changes: 14 additions & 6 deletions lib/Sidef/Variable/Class.pod
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ Return the

=cut

=head2 has_method
=head2 is_a

Class.has_method() -> I<Obj>
Class.is_a() -> I<Obj>

Return the

Aliases: I<is_an>

=cut

=head2 is_a
=head2 method

Class.is_a() -> I<Obj>
Class.method() -> I<Obj>

Return the

Aliases: I<is_an>

=cut

=head2 METHODS
Expand All @@ -57,3 +57,11 @@ Class.METHODS() -> I<Obj>
Return the

=cut

=head2 respond_to

Class.respond_to() -> I<Obj>

Return the

=cut
2 changes: 1 addition & 1 deletion lib/Sidef/Variable/ClassInit.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ package Sidef::Variable::ClassInit {
$self->__add_method__($name, $value);
}

sub has_method {
sub respond_to {
my ($self, $name) = @_;
Sidef::Types::Bool::Bool->new(exists $self->{__METHODS__}{$name});
}
Expand Down
16 changes: 8 additions & 8 deletions lib/Sidef/Variable/ClassInit.pod
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ Return the

=cut

=head2 has_method

ClassInit.has_method() -> I<Obj>

Return the

=cut

=head2 is_a

ClassInit.is_a() -> I<Obj>
Expand All @@ -87,3 +79,11 @@ Return the
Aliases: I<call>, I<init>

=cut

=head2 respond_to

ClassInit.respond_to() -> I<Obj>

Return the

=cut
2 changes: 1 addition & 1 deletion scripts/define_methods_at_runtime.sf
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var obj2 = Hello();

obj2.foo == "foo" || die "error";

if (obj2.has_method('baz')) {
if (obj2.respond_to('baz')) {
die "error";
};

Expand Down

0 comments on commit dbf29e5

Please sign in to comment.