@@ -217,9 +217,9 @@ Normally, a type constraint only checks whether the value of the parameter is of
217
217
correct type. Crucially, both I < object instances > and I < type objects > will satisfy
218
218
such a constraint as illustrated below:
219
219
220
- say 42.WHAT # OUTPUT: «( Int) »
221
- say 42 ~~ Int # OUTPUT: «True»
222
- say Int ~~ Int # OUTPUT: «True»
220
+ say 42.^name; # OUTPUT: «Int»
221
+ say 42 ~~ Int; # OUTPUT: «True»
222
+ say Int ~~ Int; # OUTPUT: «True»
223
223
224
224
Note how both C < 42 > and C < Int > satisfy the match.
225
225
@@ -246,8 +246,8 @@ its L<DEFINITE|/language/mop#DEFINITE> method.
246
246
247
247
To warm up, let's apply C < :D > to the right hand side of our humble C < Int > example:
248
248
249
- say 42 ~~ Int:D # OUTPUT: «True»
250
- say Int ~~ Int:D # OUTPUT: «False»
249
+ say 42 ~~ Int:D; # OUTPUT: «True»
250
+ say Int ~~ Int:D; # OUTPUT: «False»
251
251
252
252
Note how only C < 42 > matches C < Int:D > in the above.
253
253
@@ -269,8 +269,8 @@ constraint, which checks whether the value passed is a type object
269
269
rather than an object instance. Here's our C < Int > example again, this
270
270
time with C < :U > applied:
271
271
272
- say 42 ~~ Int:U # OUTPUT: «False»
273
- say Int ~~ Int:U # OUTPUT: «True»
272
+ say 42 ~~ Int:U; # OUTPUT: «False»
273
+ say Int ~~ Int:U; # OUTPUT: «True»
274
274
275
275
Now C < 42 > fails to match C < Int:U > while C < Int > succeeds.
276
276
@@ -288,6 +288,7 @@ Here's a more practical example:
288
288
Calling C < can-turn-into > with an object instance as its second parameter
289
289
will yield a constraint violation as intended:
290
290
291
+ = for code :preamble< sub can-turn-into(Str $, Any:U $) {...}>
291
292
say can-turn-into("a string", 123);
292
293
# OUTPUT: «Parameter '$type' of routine 'can-turn-into' must be a type object of type 'Any', not an object instance of type 'Int'...»
293
294
0 commit comments