Help language development. Donate to The Perl Foundation

MetamodelX::Dataclass zef:jjmerelo last updated on 2022-01-22


Dataclasses in Raku

Dataclasses have all the mechanisms that classes have (can be inherited, have accessors), but they don't have any method. Inspired by Python dataclasses. With private instance variables, they will be effectively frozen.

There's probably an use case for this somewhere, but this was created mainly to illustrate the metamodel.


zef install dataclass


Pretty much as any other class... Except no methods (only submethods):

use lib ".";

use MetamodelX::Dataclass;

dataclass Foo {
    has $.bar;
    has $.baz = 33;

my $foo =;

say $;

# Can be created by simply using the class name
my $bar = Foo(:33bar);

Check out also the resources/examples directory for (possibly) more examples and a negative example.

A dataclass will not compile if it's got any method, and will throw if a method is added to it dynamically.

See also

There's not a lot of information about the Metamodel in the documentation, but you can check out [the docs for ClassHOW](https://docs.raku. org/type/Metamodel::ClassHOW).


Licensed under the Artistic 2.0 License (the same as Raku itself).


(c) JJ Merelo, [email protected], 2022