| 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::Abstract::Interface - protocol for an interface | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | An interface is just a named base type that other types implement. Its | ||
| 19 | purpose is: | ||
| 20 | |||
| 21 | 1. to be used as a base type to declare a particular behaviour of the | ||
| 22 | new type, and to define what that behaviour entails (currently just | ||
| 23 | a set of method names that are expected to be implemented) | ||
| 24 | |||
| 25 | 2. to have the defined behaviour (the set of methods) checked at load | ||
| 26 | time of any module that implements the interface, and issue a | ||
| 27 | warning if anything is missing. | ||
| 28 | |||
| 29 | 3. to allow code to check at runtime whether objects conform to an | ||
| 30 | interface (early dynamic error detection) | ||
| 31 | |||
| 32 | Point (2) is implemented by `FP::Struct`: when defining a class via | ||
| 33 | `FP::Struct`, each given parent class is checked for the | ||
| 34 | implementation of a method `FP_Interface__method_names` via | ||
| 35 | `can()`. This method, if present, is supposed to ignore arguments and | ||
| 36 | return a list of the names of the set of methods that is required to | ||
| 37 | implement the interface. This method is called once at load time of | ||
| 38 | each module that defines such a class. | ||
| 39 | |||
| 40 | =head1 FUTURE / TODO | ||
| 41 | |||
| 42 | Not only declare and check the method names, but also the arity (how | ||
| 43 | many arguments it takes), argument names, and, once FP::Types is done, | ||
| 44 | optionally types. | ||
| 45 | |||
| 46 | At that point, offer more methods for introspection (IDE support). | ||
| 47 | |||
| 48 | Extend to generic functions that support the same "methods" on | ||
| 49 | unblessed references or non-reference values. | ||
| 50 | |||
| 51 | Perhaps (given some reason) rename FP::Abstract::* to FP::Protocol::*. | ||
| 52 | |||
| 53 | =head1 SEE ALSO | ||
| 54 | |||
| 55 | L<FP::Interfaces> | ||
| 56 | |||
| 57 | =head1 NOTE | ||
| 58 | |||
| 59 | This is alpha software! Read the status section in the package README | ||
| 60 | or on the L<website|http://functional-perl.org/>. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | package FP::Abstract::Interface; | ||
| 65 | |||
| 66 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 67 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 68 | 1 | ▶ | use warnings FATAL => 'uninitialized'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 69 | |||
| 70 | 0 | ▶ | sub FP_Interface__method_names { |
|
Subroutine
FP_Interface__method_names: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 71 | 0 | my $class = shift; | |
| 72 | |||
| 73 | # If we're extending another interface (not the case here though), | ||
| 74 | # we need to merge its interface definition with ours: | ||
| 75 | 0 | ((), $class->SUPER::FP_Interface__method_names) | |
| 76 | } | ||
| 77 | |||
| 78 | 1 | 1 | |