=begin pod

=TITLE class Attribute

=SUBTITLE Member variable

    class Attribute { }

In Perl 6 lingo, an I<attribute> refers to a per-instance/object storage slot.
An C<Attribute> is used to talk about classes' and roles' attributes on the
meta level.

Normal usage of attributes does not require the user to use class Attribute
explicitly.

The usual way to obtain an object of type C<Attribute> is by introspection:

    class Useless {
        has @!things;
    }
    my $a = Useless.^attributes(:local)[0];
    say $a.name;            # @!things
    say $a.package;         # Useless()
    say $a.has_accessor;    # False

    # modifying an attribute from the outside
    # this is usually not possible, but since Attribute
    # is at the level of the meta class, all is fair game
    my $instance = Useless.new;
    $a.set_value($instance, [1, 2, 3]);
    say $a.get_value($instance);        # 1 2 3

=head1 Methods

=head2 method name

Defined as:

    method name(Attribute:D:) returns Str:D

Usage:

    ATTRIBUTE.name

Returns the name of the attribute. Note that this is always the private name,
so if an attribute is declared as C<has $.a>, the name returned is C<$!a>.

=head2 method package

Defined as:

    method package(Attribute:D:) returns Mu:U

Usage:

    ATTRIBUTE.package

Returns the package (class/grammar/role) to which this attribute belongs.

=head2 method has_accessor

Defined as:

    method has_accessor(Attribute:D:) returns Bool:D

Usage:

    ATTRIBUTE.has_accessor

Returns C<True> if the attribute has a public accessor method.

=head2 method readonly

Defined as:

    method readonly(Attribute:D:) returns Bool:D

Usage:

    ATTRIBUTE.readonly

Returns C<True> for readonly attributes, which is the default.
Returns C<False> for attributes marked as C<is rw>.

=head2 method type

Defined as:

    method type(Attribute:D:) returns Mu

Usage:

    ATTRIBUTE.type

Returns the type constraint of the attribute.

=head2 method get_value

Defined as:

    method get_value(Attribute:D: Mu $instance)

Usage:

    my $val = ATTRIBUTE.get_value(OBJECT)

Returns the value stored in this attribute of object C<$instance>.

Note that this method violates encapsulation of the object, and should be
used with care. Here be dragons.

=head2 method set_value

Defined as:

    method set_value(Attribute:D: Mu $instance, Mu \new_val)

Usage:

    ATTRIBUTE.set_value(OBJECT, NEW_VALUE)

Binds the value C<new_val> to this attribute of object C<$instance>.

Note that this method violates encapsulation of the object, and should be
used with care. Here be dragons.

=head2 trait is required

    multi sub trait_mod:<is> (Attribute $attr, :$required!)

Marks an attribute as being required. If a value is not provided
during object construction, an exception is thrown.

    class Foo {
       has $.bar is required;
       has $.baz;
    }

    Foo.new(bar => 42) # works
    Foo.new(baz => 42) # fails - bar is required

C<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 L<bless>.


=head2 trait is rw

    multi sub trait_mod:<is> (Attribute:D $attr, :$rw!)

Marks an attribute as read/write as opposed to the default
C<readonly>. The default accessor for the attribute will return a
writable value.

   class Foo {
      has $.bar is rw;
      has $.baz;
   }

   my $foo = Foo.new;
   $foo.bar = 42; # works
   $foo.baz = 42; # fails

=end pod
