Skip to content

Commit

Permalink
type? now creates a lazy type interface. if an object complies once, …
Browse files Browse the repository at this point in the history
…it complies forever #36
  • Loading branch information
cooper committed Nov 20, 2016
1 parent 45ae4ae commit 90d0a79
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/F/Function.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ sub desc {
return "function '$$func{name}'";
}

# all WantNeeds which belong to this function
sub arguments {
my $func = shift;

Expand All @@ -53,6 +54,7 @@ sub arguments {
return @wn;
}

# all Returns and ReturnPairs which belong to this function
sub returns {
my $func = shift;

Expand Down
2 changes: 1 addition & 1 deletion lib/F/Regex.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ sub desc {
my $first = length $rgx->{value} > 13 ?
substr($rgx->{value}, 0, 10).'...' :
$rgx->{value};
return "Regex /$first/";
return "regex /$first/";
}

sub perl_fmt {
Expand Down
18 changes: 14 additions & 4 deletions lib/Ferret/Core/FF.pm
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ sub method_event_def {
# type definitions.
sub typedef {
my ($scope, $scope_or_class, $type_name, $code, $lazy) = @_;
my $typedef = sub {
my $f = $scope->f;
my $create_typedef = sub {

# this sub returns a function which returns Ferret true if
# a method requirement is satisfied.
Expand Down Expand Up @@ -530,21 +531,30 @@ sub typedef {

};

# create a prototype.
my $proto = Ferret::Prototype->new($f) if $lazy;

# create a function.
my $func = ffunction(sub {
my (undef, $args) = @_;
my $obj = $args->{obj};

# already an instance of this type.
return $obj if $proto && $obj->has_parent($proto);

# compute the result and make it an instance of this type.
my $res = $code->($obj, $create_can, $transform);
$res->add_parent($proto) if $proto;

return $res || Ferret::undefined;
}, $type_name, '$obj');

$func->set_property(proto => $proto) if $proto;
$func->{is_typedef} = 1;
return $func;
};

$scope_or_class->set_property($type_name =>
$lazy ? [ $typedef ] : $typedef->()
); # TODO: pos
$scope_or_class->set_property($type_name => $create_typedef->());
}

sub typedef_check {
Expand Down
2 changes: 1 addition & 1 deletion lib/Ferret/Object.pm
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ sub _uniq {
sub parent_classes {
my ($obj, @classes) = shift;
foreach my $parent ($obj->parents) {
next unless $parent->{is_proto};
next unless $parent->{is_proto} && $parent->{proto_class};
push @classes, $parent->{proto_class};
}
return @classes;
Expand Down

0 comments on commit 90d0a79

Please sign in to comment.