| 1 | # | ||||||||||
| 2 | # Copyright (c) 2019 Christian Jaeger, copying@christianjaeger.ch | ||||||||||
| 3 | # | ||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||
| 7 | # bundled with this file. | ||||||||||
| 8 | # | ||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | FP::Struct::Equal -- automatic Equal protocol implementation | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | package FP_Struct_Equal_Example::Foo { | ||||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | use FP::Struct ["a","b"], | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | 'FP::Struct::Equal'; | |||||||||
| 20 | |||||||||||
| 21 | _END_ | ||||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | FP_Struct_Equal_Example::Foo::constructors->import; | ||||||||||
| 25 | 1 | ▶ | use FP::Equal; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | ok equal(Foo(1, [1+1, 3]), Foo(1, [2, 3])); | ||||||||||
| 27 | ok not equal(Foo(1, 1+1), Foo(1, [3])); | ||||||||||
| 28 | |||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||
| 30 | |||||||||||
| 31 | This class, when listed as a superclass of an L<FP::Struct>, | ||||||||||
| 32 | automatically implements the L<FP::Abstract::Equal> protocol | ||||||||||
| 33 | (i.e. generates an `FP_Equal_equal` method that uses inspection | ||||||||||
| 34 | specific to FP::Struct classes to get to know the public field values | ||||||||||
| 35 | of the object it is being called on, and reconstructs a constructor | ||||||||||
| 36 | call based on this information.) This will be the right thing for the | ||||||||||
| 37 | typical `FP::Struct` based class that don't have or mutate hidden | ||||||||||
| 38 | fields or want to exclude some fields from equality tests. | ||||||||||
| 39 | |||||||||||
| 40 | =head1 SEE ALSO | ||||||||||
| 41 | |||||||||||
| 42 | Creates implementations for: L<FP::Abstract::Equal> | ||||||||||
| 43 | |||||||||||
| 44 | L<FP::Struct::Show> | ||||||||||
| 45 | |||||||||||
| 46 | =head1 NOTE | ||||||||||
| 47 | |||||||||||
| 48 | This is alpha software! Read the status section in the package README | ||||||||||
| 49 | or on the L<website|http://functional-perl.org/>. | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | package FP::Struct::Equal; | ||||||||||
| 54 | |||||||||||
| 55 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 56 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 5 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 58 | |||||||||||
| 59 | 5 | ▶ | use base 'FP::Abstract::Equal'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 60 | 5 | ▶ | use FP::Equal (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 61 | |||||||||||
| 62 | 45 | ▶ | sub FP_Equal_equal { | ||||||||
|
Subroutine
FP_Equal_equal: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 63 | 45 | my ($self, $b) = @_; | |||||||||
| 64 | 45 | my $class = ref($self); | |||||||||
| 65 | 45 | ▶ | length $class or die "FP_Show_show called on non-object: $self"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 45 | my $fieldnames = do { | |||||||||
| 67 | 5 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 68 | 45 | \@{"${class}::__Struct__fields"} | |||||||||
| 69 | }; | ||||||||||
| 70 | |||||||||||
| 71 | # XX is all_fields slow, probably? Optim? | ||||||||||
| 72 | 45 | for (FP::Struct::all_fields([$class])) { | |||||||||
| 73 | 119 | my $fieldname = FP::Struct::field_name($_); | |||||||||
| 74 | 119 | ▶ | FP::Equal::equal($self->$fieldname, $b->$fieldname) or return 0 | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | } | ||||||||||
| 76 | 1 | ||||||||||
| 77 | 44 | } | |||||||||
| 78 | |||||||||||
| 79 | 5 | 1 | |||||||||