Skip to content

Commit

Permalink
Introduce Val::Object, make it the top type
Browse files Browse the repository at this point in the history
This re-establishes Val::Object, but this time as the kind of
empty type that goes at the top of a type hierarchy.

As a consequence, "everything is an Object", which right now is
hard-coded into the ~~ and !~~ operators.

In the fullness of time, we'll instead code it into the type
hierarchy itself, so that a special case is not needed.

Closes #144.
  • Loading branch information
Carl Masak committed Feb 11, 2019
1 parent 2aca4a7 commit 77a8834
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/_007/Builtins.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ my @builtins =
sub ($lhs, $rhs) {
assert-type(:value($rhs), :type(Val::Type), :operation<~~>);

return wrap($lhs ~~ $rhs.type);
return wrap($rhs.type ~~ Val::Object || $lhs ~~ $rhs.type);
},
:qtype(Q::Infix),
:precedence{ equiv => "infix:==" },
Expand All @@ -158,7 +158,7 @@ my @builtins =
sub ($lhs, $rhs) {
assert-type(:value($rhs), :type(Val::Type), :operation<!~~>);

return wrap($lhs !~~ $rhs.type);
return wrap($rhs.type !~~ Val::Object && $lhs !~~ $rhs.type);
},
:qtype(Q::Infix),
:precedence{ equiv => "infix:==" },
Expand Down
10 changes: 10 additions & 0 deletions lib/_007/Val.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ class Val::None does Val {

constant NONE is export = Val::None.new;

### ### Object
###
### The top type of 007. A featureless object. Everything inherits from this type.
class Val::Object does Val {
method truthy {
True
}
}

### ### Bool
###
### A type with two values, `true` and `false`. These are often the result
Expand Down Expand Up @@ -583,6 +592,7 @@ class Val::Exception does Val {
class Helper {
our sub Str($_) {
when Val::None { "none" }
when Val::Object { "<object>" }
when Val::Bool { .value.lc }
when Val::Int { .value.Str }
when Val::Str { .value }
Expand Down
10 changes: 10 additions & 0 deletions t/features/types.t
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,14 @@ use _007::Test;
"Q is a built-in type (#201)";
}

{
outputs 'my o = new Object {}; say(o); say(type(o))', "<object>\n<type Object>\n",
"can create a new Object";
}

{
outputs 'for [42, [], "", Int, Array, Type, Object] -> x { say(x ~~ Object) }', "true\n" x 7,
"everything is an object";
}

done-testing;

0 comments on commit 77a8834

Please sign in to comment.