Documentation for routine
is assembled from the following types:
proto sub trait_mod:<is>(Mu $, |)
is applies to any kind of scalar object, and can take any number of named or positional arguments. It is the most commonly used trait, and takes the following forms, depending on the type of the first argument.
The most common form, involving two classes, one that is being defined and the other existing, defines parenthood.
A is B, if both are classes, defines A as a subclass of B.
is DEPRECATED can be applied to classes, Attributes or Routines, marks them as deprecated and issues a message, if provided.
Several instances of
is are translated directly into attributes for the class they refer to:
The Uninstantiable representation trait is not so much related to the representation as related to what can be done with a specific class; it effectively prevents the creation of instances of the class in any possible way.
constant = <Innie Minnie Moe>;is repr('Uninstantiable')say don't-instantiate.imm for ^10;
Uninstantiable classes can still be used via their class variables and methods, as above. However, trying to instantiate them this way:
my $do-instantiate = don't-instantiate.new; will yield the error
You cannot create an instance of this type (don't-instantiate).
is trait refers, in general, to the nature of the class or object they are applied to, they are used extensively in native calls to specify the representation of the data structures that are going to be handled by the native functions via the
is repr suffix; at the same time,
is native is used for the routines that are actually implemented via native functions. These are the representations that can be used:
CStruct corresponds to a
structin the C language. It is a composite data structure which includes different and heterogeneous lower-level data structures; see this for examples and further explanations.
CPPStruct, similarly, correspond to a
structin C++. However, this is Rakudo specific for the time being.
On the other hand, P6opaque is the default representation used for all objects in Raku.
;say Thar.REPR; # OUTPUT: «P6opaque␤»
The metaobject protocol uses it by default for every object and class unless specified otherwise; for that reason, it is in general not necessary unless you are effectively working with that interface.
is trait can be used on the definition of methods and routines to establish precedence and associativity. They act as a sub defined using
trait_mod which take as argument the types and names of the traits that are going to be added. In the case of subroutines, traits would be a way of adding functionality which cuts across class and role hierarchies, or can even be used to add behaviors to independently defined routines.
is implementation-detail trait
Note: this trait has been available in Rakudo compiler starting from 2020.05 release.
This trait is used by Raku language implementations and module authors to mark particular routines (including methods) as not meant to be a part of public API. While such routines can be found when looked up directly, they will not appear in results of introspection:
my = CORE::<&DYNAMIC>;say CORE::.keys.grep(* eq '&DYNAMIC'); # OUTPUT: «()␤»
Such routines are not meant for use by users and their behavior and availability can be changed anytime.
method is-implementation-detail(--> True)
Applying this trait makes the
is-implementation-detail method called on Code to return
True, thus giving a hint to the user not to use it if they are not willing to maintain this code in case of changes for years to come:
my = ;unless .is-implementation-detailsub PRIVATE-CALCULATION is implementation-detailif .is-implementation-detail
multi sub is(Mu , Mu , = '')multi sub is(Mu , Mu , = '')
Marks a test as passed if
$expected compare positively with the
eq operator, unless
$expected is a type object, in which case
=== operator will be used instead; accepts an optional description of the test as the last argument.
eq operator stringifies its operands, which means
is() is not a good function for testing more complex things, such as lists:
is (1, (2, (3,))), [1, 2, 3] passes the test, even though the operands are vastly different. For those cases, use
my ; sub factorial() ; ...;is .author, "Joe", 'Retrieving the author field';is factorial(6), 720, 'Factorial - small integer';my Int ;is , Int, 'The variable $a is an unassigned Int';
Note: if only whitespace differs between the values,
is() will output failure message differently, to show the whitespace in each values. For example, in the output below, the second test shows the literal
\t in the
is "foo\tbar", "foo\tbaz"; # expected: 'foo baz'␤# got: 'foo bar'is "foo\tbar", "foo bar"; # expected: "foo bar"␤# got: "foo\tbar"