-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.pod6
78 lines (57 loc) · 2.31 KB
/
README.pod6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
=begin pod
![Build Status](https://github.com/Kaiepi/ra-Data-Record/actions/workflows/test.yml/badge.svg)
=head1 NAME
Data::Record - Record types!
=head1 SYNOPSIS
=begin code :lang<perl6>
use Data::Record;
# Data::Record introduces record types for maps, lists, and tuples:
my constant Schema = {@
name => Str:D,
items => [@ <@ Int:D, Str:D @> @]
@} :name('Schema');
# With the type we just made, we can typecheck data structures to ensure they
# match it:
my %valid =
name => 'Kaiepi',
items => [(1, 'Item 1'), (2, 'Item 2')];
my %invalid =
name => 'Mrofnet',
items => [],
constructor => 'Thanks, JavaScript.';
say %valid ~~ Schema; # OUTPUT: True
say %invalid ~~ Schema; # OUTPUT: False
# ...but typechecking this way is inefficient, and is always done eagerly!
# Using the (<<), (>>), (<>), and (><) operators provided, data can be coerced
# to our record type by various means:
%valid := %valid (><) Schema;
%invalid := %invalid (<<) Schema;
say %invalid; # OUTPUT: {items => [], name => Mrofnet}
# For the most part, coerced data can be used the same way as the original
# data, with the bonus of extra typechecking:
{
CATCH { default { say .^name } }
%invalid<items>.push: (3, 'Item 3'); # OK!
%invalid<items>.push: "OOPSIE WOOPSIE OwO"; # OUTPUT: X::Data::Record::TypeCheck
}
# Finally, to restore the data's original typing, simply call the unrecord
# method on it:
my %now-valid := %invalid.unrecord;
say %now-valid.^name; # OUTPUT: Hash
say %now-valid<items>.^name; # OUTPUT: Array
say %now-valid<items>[0].^name; # OUTPUT: List
=end code
=head1 DESCRIPTION
C<Data::Record> is a library that adds support for record types to Raku.
Operators for creating record types for maps, lists, and tuples are included.
Data structures can then be coerced to these record types using the coercion
operators provided, some of which will sanitize them, all of which allow for
efficient typechecking for common operations you can do with them.
For documentation on how this library can be used, refer to the
L<wiki|https://github.com/Kaiepi/p6-Data-Record/wiki/>.
=head1 AUTHOR
Ben Davies (Kaiepi)
=head1 COPYRIGHT AND LICENSE
Copyright 2022 Ben Davies
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
=end pod