3 ms·
Types as documentation is one of the things I like best about Perl6 types. Perl6 has the concept of a `subset` of a type. Let's say I want to write a function
by daotoad 8y ago
Types as documentation is one of the things I like best about Perl6 types. Perl6 has the concept of a `subset` of a type. Let's say I want to write a function that takes a name which must be only one line of text, and a positive integer as arguments.
In Perl6 I can document those requirements in the subroutine signature:
sub foo( Str $name where *.lines == 1, Int $count where * > 0 ) {
say "Name is $name with count $count";
}
Or if I want to name the concepts and reuse them, I can say:
subset LegalName of Str where *.lines == 1;
subset PositiveInteger of Int where * > 0;
sub bar( LegalName $name, PositiveInteger $count ) { foo( $name, $count) }
You can use these subsets for multiple dispatch as well:
# Print a single line argument.
multi sub print-it( Str:D $it where *.lines == 1 ) {
say $it;
}
# Print a multiline string as a single line
multi sub print-it( Str:D $it ) {
say $it.lines.join('');
}
# Handle anything that we didn't expect
multi sub print-it( Any $it ) {
Failure.new( "it was unprintable", $it.gist );
}
IME, this makes it easy to write self documenting code.
- DonHopkins 8y agoThen you'd probably love ADA! Does Perl 6 have postconditions, too? https://www.adacore.com/gems/gem-31 https://www.adacore.com/gems/gem-31 Gem #31: Preconditions/postconditions >The notion of preconditions and postconditions is an old one. A precondition is a condition that must be true before a section of code is executed, and a postcondition is a condition that must be true after the section of code is executed. http://www.ada-auth.org/standards/12rat/html/Rat12-2-3.html http://www.ada-auth.org/standards/12rat/html/Rat12-2-3.html >We will look first at the simple case when inheritance is not involved and then look at more general cases. Specific preconditions and postconditions are applied using the aspects Pre and Post respectively whereas class wide conditions are applied using the aspects Pre'Class and Post'Class. >To apply a specific precondition Before and/or a specific postcondition After to a procedure P we write procedure P(P1: in T1; P2: in out T2; P3: out T3) with Pre => Before, Post => After; >where Before and After are expressions of a Boolean type (that is of type Boolean or a type derived from it). If your language supports preconditions, postconditions and invariants, then you can pursue the "Design by Contract" approach to programming, coined by Bertrand Meyer for Eiffel. That's when you guarantee that if all the preconditions are met before calling a function, then all the postconditions will be met after it returns (but the object may go through intermediate states where the postconditions aren't met before it returns, like while inserting an item into a doubly linked list). Unfortunately that idea breaks down when you have multiple threads that might enter a function at the same time, so you have to use object level locks, which have their own problems. https://en.wikipedia.org/wiki/Design_by_contract https://en.wikipedia.org/wiki/Design_by_contract https://en.wikipedia.org/wiki/Eiffel_(programming_language) https://en.wikipedia.org/wiki/Eiffel_(programming_language) >The design of the language is closely connected with the Eiffel programming method. Both are based on a set of principles, including design by contract, command–query separation, the uniform-access principle, the single-choice principle, the open–closed principle, and option–operand separation. >Many concepts initially introduced by Eiffel later found their way into Java, C#, and other languages. New language design ideas, particularly through the Ecma/ISO standardization process, continue to be incorporated into the Eiffel language.
- lizmat 8y agoYes, Perl 6 has pre and post-conditions. They are available in the form of the `PRE` and `POST` phasers: https://docs.perl6.org/language/phasers#PRE https://docs.perl6.org/language/phasers#PRE , https://docs.perl6.org/language/phasers#POST https://docs.perl6.org/language/phasers#POST