Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
distinguish use of "default" as adjective or noun
  • Loading branch information
tbrowder committed Mar 30, 2019
1 parent ec2b724 commit 11e5617
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions doc/Type/Signature.pod6
Expand Up @@ -113,6 +113,7 @@ These can be used to restrict the allowed input to a function.
my $sig = :(Int $a, Str $b);
Type constraints can have any compile-time defined value
=begin code
subset Positive-integer of Int where * > 0;
sub divisors(Positive-integer $n) { $_ if $n %% $_ for 1..$n };
Expand Down Expand Up @@ -150,7 +151,7 @@ a true value to pass the type check
sub f(Real $x where { $x > 0 }, Real $y where { $y >= $x }) { }
The code in C<where> clauses has some limitations: anything that produces
side-effects (e.g. printing output, pulling from an iterator, or increasing a
side-effects (e.g., printing output, pulling from an iterator, or increasing a
state variable) is not supported and may produce surprising results if used.
Also, the code of the C<where> clause may run more than once for a single
typecheck in some implementations.
Expand All @@ -176,11 +177,11 @@ say, have several conditions:
-> $y where { .so && .name } {}( sub two {} ); # OK!
-> $y where .so & .name.so {}( sub three {} ); # Also good
The first version is wrong and will issue a warning about sub object coerced
The first version is wrong and will issue a warning about a sub object coerced
to string. The reason is the expression is equivalent to
C<($y ~~ ($y.so && $y.name))>; that is "call C<.so>, and if that is C<True>,
call C<.name>; if that is also C<True> use its value for smartmatching…". It's
the B<result> of C<(.so && .name)> is will be smartmatched against, but we
the B<result> of C<(.so && .name)> it will be smartmatched against, but we
want to check that both C<.so> and C<.name> are truthy values. That is why
an explicit Block or a L<Junction|/type/Junction> is the right version.
Expand Down Expand Up @@ -349,12 +350,12 @@ document further elaborates on the concepts of instances and type
objects and discovering them with the C<.DEFINITE> method.
Keep in mind all parameters have values; even optional ones have default
defaults that are the type object of the constrained type for explicit type
constraints. If no explicit type constraint exists, the default default is an
values that are the type object of the constrained type for explicit type
constraints. If no explicit type constraint exists, the default value is an
L<Any|/type/Any> type object for methods, submethods, and subroutines, and a
L<Mu|/type/Mu> type object for blocks. This means that if you use the C<:D>
type smiley, you'd need to provide a default value or make the parameter
required. Otherwise, the default default would be a type object, which would
required. Otherwise, the default value would be a type object, which would
fail the definiteness constraint.
sub divide (Int:D :$a = 2, Int:D :$b!) { say $a/$b }
Expand All @@ -372,17 +373,17 @@ positional or named, gets no value I<at all>.
f; # OUTPUT: «42␤42␤»
f Nil; # OUTPUT: «Nil␤answer␤»
C<$a> has 42 as default value. With no value, C<$a> will be assigned the
default declared in the Signature. However, in the second case, it
C<$a> has 42 as its default value. With no value, C<$a> will be assigned the
default value declared in the Signature. However, in the second case, it
I<does> receive a value, which happens to be C<Nil>. Assigning C<Nil> to
any variable resets it to its default value, which has been declared as
C<'answer'>. That explains what happens the second time we call C<f>.
C<'answer'> by use of the I<default> trait. That explains what happens the second time we call C<f>.
Routine parameters and variables deal differently with default value,
which is in part clarified by the different way default values are
declared in each case (using C<=> for parameters, using the C<default>
trait for variables).
Note: in 6.c language, the default default of C<:U>/C<:D> constrained
Note: in 6.c language, the default value of C<:U>/C<:D> constrained
variables was a type object with such a constraint, which is not initializable,
thus you cannot use the C<.=> operator, for example.
Expand All @@ -392,7 +393,7 @@ my Int:D $x .= new: 42;
# OUTPUT: You cannot create an instance of this type (Int:D)
# in block <unit> at -e line 1
In the 6.d language, the default default is the type object without the smiley
In the 6.d language, the default I<default> is the type object without the smiley
constraint:
=for code :solo
Expand Down

0 comments on commit 11e5617

Please sign in to comment.