ValueClass

A way to create immutable value objects

NAME

ValueClass - A way to create immutable value objects

SYNOPSIS

use ValueClass;

value-class Bla {
    has $.a = 42;
    has @.b;
    has %.c;

    method TWEAK() {
        %!c := ValueMap.new: (a => 1)
    }
}

say Bla.new: :b[1,2,3];
# Bla.new(a => 42, b => Tuple.new(1, 2, 3), c => ValueMap.new((:a(1))))

DESCRIPTION

ValueClass creates immutable objects.

If you are only worried about other people mutating your objects, you may take a look at ValueType. But if you want to avoid letting even yourself, on your internal methods, mutate your objects, you will probably need something like this module.

Classes created using the value-class keyword will create objects that will die whenever anyone try to mutate them. It will also die when the object is created with any attribute that's not a value type.

The object will become immutable just after TWEAK. So TWEAK is your last chance to mutate your objects.

(It does not allow default values for @ and % sigled attributes. You will need to use TWEAK to populate them)

ValueType will change the default type for a attribute using the @ to Tuple and the % to ValueMap to make it possible to use Positionals and Associative on ValueClass.

AUTHOR

Fernando CorrĂȘa de Oliveira [email protected]

COPYRIGHT AND LICENSE

Copyright 2024 Fernando CorrĂȘa de Oliveira

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.

ValueClass v0.0.10

A way to create immutable value objects

Authors

  • Fernando CorrĂȘa de Oliveira

License

Artistic-2.0

Dependencies

Tuple:ver<0.0.10>:auth<zef:lizmat>ValueMap:ver<0.0.2>:auth<zef:lizmat>

Test Dependencies

Provides

  • MetamodelX::ValueClassHOW
  • ValueClass
  • ValueClass::Attribute

Documentation

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.

Built with Podlite — the markup and publishing tools behind this site.