In Raku lingo, an attribute refers to a per-instance/object storage slot. An Attribute
is used to talk about classes' and roles' attributes at the metalevel.
Normal usage of attributes does not require the user to use this class explicitly.
Traits§
Trait is default§
An attribute that is assigned Nil
will revert to its default value set with the trait is default
. In the case of arrays or associatives, the argument of is default
will set the default item value or hash value.
my = C.new;say ;.a = Nil;say ;# OUTPUT: «C.new(a => 666)C.new(a => 42)»;my = Foo.new( bar => <a b c> );.bar =Nil;say ; # OUTPUT: «Foo.new(bar => [42])»
Trait is required§
multi trait_mod:<is> (Attribute , :!)
The trait is required
will mark the attribute as to be filled with a value when the object is instantiated. Failing to do so will result in a runtime error.
my = C.new;CATCH# OUTPUT: «X::Attribute::Required: The attribute '$!a' is required, but you did not provide a value for it.»
This trait also allows attributes to be typed with types that have a :D
smiley without giving them a default value:
Available as of 6.d language version (early implementation exists in Rakudo compiler 2018.08+): You can specify a reason why the attribute is required:
my = D.new;CATCH# OUTPUT: «X::Attribute::Required: The attribute '$!a' is required because it is a good idea,but you did not provide a value for it.»
is required
doesn't just affect the default constructor, it checks for the attribute at a lower level, so it will work for custom constructors written using bless.
trait is DEPRECATED§
multi trait_mod:<is>(Attribute , :!)
Marks an attribute as deprecated, optionally with a message what to use instead.
my = C.new( foo => 42 ); # doesn't trigger with initialization (yet)say .foo; # does trigger on usage
After the program is finished, this will show something like this on STDERR:
# Saw 1 occurrence of deprecated code.# =====================================# Method foo (from C) seen at:# script.raku, line 5# Please use 'bar' instead.
trait is rw§
multi trait_mod:<is> (Attribute , :!)
Marks an attribute as read/write as opposed to the default readonly
. The default accessor for the attribute will return a writable value.
;my = Boo.new;.bar = 42; # works.baz = 42;CATCH ;# OUTPUT: «X::Assignment::RO: Cannot modify an immutable Any»
trait is built§
multi trait_mod:<is>(Attribute , :!)
By default, this trait allows setting up a private attribute during object construction via .new
. The same trait can be used to prevent setting up a public attribute via .new
by passing it the Boolean value False
.
my = Foo.new(bar => 1, baz => 2);say .bar; # OUTPUT: «1»say .baz; # OUTPUT: «Any»
Available as of the 2020.01 release of the Rakudo compiler.
Methods§
The usual way to obtain an object of type Attribute
is by introspection:
my = Useless.^attributes(:local)[0];say .raku; # OUTPUT: «Attribute.new»say .name; # OUTPUT: «@!things»say .package; # OUTPUT: «(Useless)»say .has_accessor; # OUTPUT: «False»
Modifying a private attribute from the outside is usually not possible, but since Attribute is at the level of the metaclass, all is fair game.
method name§
method name(Attribute: --> Str)
Returns the name of the attribute. Note that this is always the private name, so if an attribute is declared as has $.a
, the name returned is $!a
.
my = Foo.^attributes(:local)[0];say .name; # OUTPUT: «@!bar»
method package§
method package()
Returns the package (class/grammar/role) to which this attribute belongs.
my = Boo.^attributes(:local)[0];say .package; # OUTPUT: «(Boo)»
method has_accessor§
method has_accessor(Attribute: --> Bool)
Returns True
if the attribute has a public accessor method.
my = Container.^attributes(:local)[0];my = Container.^attributes(:local)[1];say .has_accessor; # OUTPUT: «False»say .has_accessor; # OUTPUT: «True»
method rw§
method rw(Attribute: --> Bool)
Returns True
for attributes that have the "is rw" trait applied to them.
my = Library.^attributes(:local)[0];my = Library.^attributes(:local)[1];say .rw; # OUTPUT: «False»say .rw; # OUTPUT: «True»
method readonly§
method readonly(Attribute: --> Bool)
Returns True
for readonly attributes, which is the default, or False
for attributes marked as is rw
.
my = Library.^attributes(:local)[0];my = Library.^attributes(:local)[1];say .readonly; # OUTPUT: «True»say .readonly; # OUTPUT: «False»
method required§
method required(Attribute: --> Any)
Returns 1
for attributes that have the "is required" trait applied, or Mu
if the attribute did not have that trait applied. If the "is required" trait is applied with a string, then that string will be returned instead of 1
.
my = Library.^attributes(:local)[0];my = Library.^attributes(:local)[1];say .required; # OUTPUT: «1»say .readonly; # OUTPUT: «"we always need more books"»
method type§
method type(Attribute: --> Mu)
Returns the type constraint of the attribute.
my = TypeHouse.^attributes(:local)[0..2];for 0..2# OUTPUT: «(Positional[Int])# (Mu)# (Positional)»
method get_value§
method get_value(Mu )
Returns the value stored in this attribute of object $obj
.
my = Violated.^attributes(:local)[0];say .get_value(Violated.new); # OUTPUT: «5»
Note that this method violates encapsulation of the object, and should be used with care. Here be dragons.
method set_value§
method set_value(Mu , Mu \new_val)
Binds the value new_val
to this attribute of object $obj
.
my = A.^attributes(:local)[0];my = A.new;.speak; # OUTPUT: «5».set_value(, 42);.speak; # OUTPUT: «42»
Note that this method violates encapsulation of the object, and should be used with care. Here be dragons.
method gist§
multi method gist(Attribute:)
Returns the name of the type followed by the name of the attribute.
say Hero.^attributes(:local)[0]; # OUTPUT: «Positional @!inventory»
Since say implicitly calls .gist
, that is what produces the output here.
Optional introspection§
DEPRECATED§
If an attribute is marked as DEPRECATED
, then calling the DEPRECATED
method is possible and will return "something else"
(if no specific reason was specified) or the string that was specified with the DEPRECATED
trait.
If an attribute is not marked as DEPRECATED, one cannot call the DEPRECATED
method. Therefore, the .?method
syntax should be used.
my = Hangout.^attributes(:local)[0];my = Hangout.^attributes(:local)[1];with .?DEPRECATED ->with .?DEPRECATED ->