Skip to content

Commit aa2ae69

Browse files
committed
Document types in the JS nutshell
1 parent b3480e1 commit aa2ae69

File tree

1 file changed

+150
-3
lines changed

1 file changed

+150
-3
lines changed

doc/Language/js-nutshell.pod6

Lines changed: 150 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=begin pod :tag<convert>
22
3-
=TITLE Javascript (Node.js) to Perl 6 - nutshell
3+
=TITLE JavaScript (Node.js) to Perl 6 - nutshell
44
55
=SUBTITLE Learning Perl 6 from Node.js, in a nutshell
66
@@ -609,9 +609,150 @@ say VERSION_NUMBER; # OUTPUT: 33554432
609609
610610
=head2 Types
611611
612-
=comment TODO
612+
=head3 Creating types
613613
614-
# TBD
614+
In JavaScript, types are created by making a class (or a constructor in ES5
615+
and earlier). If you've used TypeScript, you can define a type as a subset of
616+
other types like so:
617+
618+
=begin code :lang<typescript>
619+
type ID = string | number;
620+
=end code
621+
622+
In Perl 6, classes, roles, subsets, and enums are considered types. Creating
623+
classes and roles will be discussed in
624+
L<the OOP section of this article|#Object-oriented_programming>. Creating an ID
625+
subset can be done like so:
626+
627+
=begin code
628+
subset ID where Str | Int;
629+
=end code
630+
631+
See the documentation on L<subset|/language/typesystem#subset> and
632+
L<Junction|/type/Junction> for more information.
633+
634+
TypeScript enums may have numbers or strings as their values. Defining the
635+
values is optional; by default, the value of the first key is 0, the next key,
636+
1, the next, 2, etc. For example, here is an enum that defines directions for
637+
extended ASCII arrow symbols (perhaps for a TUI game):
638+
639+
=begin code :lang<typescript>
640+
enum Direction (
641+
UP = '↑',
642+
DOWN = '↓',
643+
LEFT = '←',
644+
RIGHT = '→'
645+
);
646+
=end code
647+
648+
Enums in Perl 6 may have any type as their keys' values. Enum keys (and
649+
optionally, values) can be defined by writing C<enum>, followed by the name
650+
of the enum, then the list of keys (and optionally, values), which can be done
651+
using L«< >|/language/quoting#Word_quoting:_<_>»,
652+
L<« »|/language/quoting#Word_quoting_with_interpolation_and_quote_protection:_«_»>,
653+
or L<( )|/language/operators#term_(_)>. C<( )> must be used if you want to
654+
define values for the enum's keys. Here is the Direction enum as written in
655+
Perl 6:
656+
657+
=begin code
658+
enum Direction (
659+
UP => '↑',
660+
DOWN => '↓',
661+
LEFT => '←',
662+
RIGHT => '→'
663+
);
664+
=end code
665+
666+
See the documentation on L<enum|/language/typesystem#enum> for more information.
667+
668+
=head3 Using types
669+
670+
In TypeScript, you can define the type of variables. Attempting to assign a
671+
value that doesn't match the type of the variable will make the transpiler
672+
error out. This is done like so:
673+
674+
=begin code :lang<typescript>
675+
enum Name (Phoebe, Daniel, Joe);
676+
let name: string = 'Phoebe';
677+
name = Phoebe; # Causes tsc to error out
678+
679+
let hobbies: [string] = ['origami', 'playing instruments', 'programming'];
680+
681+
let todo: Map<string, boolean> = new Map([
682+
['clean the bathroom', false],
683+
['walk the dog', true],
684+
['wash the dishes', true]
685+
]);
686+
=end code
687+
688+
In Perl 6, variables can be typed by placing the type between the declarator
689+
(C<my>, C<our>, etc.) and the variable name. Assigning a value that doesn't
690+
match the variable's type will throw either a compile-time or runtime error,
691+
depending on how the value is evaluated:
692+
693+
=begin code :skip-test
694+
enum Name <Phoebe Daniel Joe>;
695+
my Str $name = 'Phoebe';
696+
$name = Phoebe; # Throws a compile-time error
697+
698+
# The type here defines the type of the elements of the array.
699+
my Str @hobbies = ['origami', 'playing instruments', 'programming'];
700+
701+
# The type here defines the type of the keys of the hash.
702+
# The type in the curly braces defines the type of the values of the hash.
703+
my Str %todo{Bool} = (
704+
'clean the bathroom' => False,
705+
'walk the dog' => True,
706+
'wash the dishes' => True
707+
);
708+
=end code
709+
710+
=head3 Comparing JavaScript and Perl 6 types
711+
712+
Here is a table of some JavaScript types and their equivalents in Perl 6:
713+
714+
=begin table
715+
JavaScript | Perl 6
716+
=============================
717+
Object | Mu, Any, Hash
718+
Array | List, Array, Seq
719+
String | Str
720+
Number | Int, Num, Rat
721+
Boolean | Bool
722+
Map | Map, Hash
723+
Set | Set, SetHash
724+
=end table
725+
726+
C<Object> is both a superclass of all types in JavaScript and a way to create
727+
a hash. In Perl 6, L<Mu|/type/Mu> is a superclass of all types, though usually
728+
you want to use L<Any|/type/Any> instead, which is a subclass of C<Mu> but also
729+
a superclass of nearly every type, with L<Junction|/type/Junction> being an
730+
exception. When using C<Object> as a hash, L<Hash|/type/Hash> is what you want
731+
to use.
732+
733+
There are three types equivalent to C<Array>. L<List|/type/List> is most similar
734+
to C<Array>, since it acts as a mutable array. L<Array|/type/Array> is similar
735+
to C<List>, but is immutable. L<Seq|/type/Seq> is used to create lazy arrays.
736+
737+
C<String> and L<Str|/type/Str> are for the most part used identically.
738+
739+
There are several different types in Perl 6 equivalent to C<Number>, but the
740+
three you'll most commonly see are L<Int|/type/Int>, L<Num|/type/Num>, and
741+
L<Rat|/type/Rat>. C<Int> represents an integer. C<Num> represents a
742+
floating-point number, making it the most similar to C<Number>. C<Rat>
743+
represents a fraction of two numbers, and is used when C<Num> cannot provide
744+
precise enough values.
745+
746+
C<Boolean> and L<Bool|/type/Bool> are for the most part used identically.
747+
748+
C<Map> has both a mutable and an immutable equivalent in Perl 6.
749+
L<Map|/type/Map> is the immutable one, and L<Hash|/type/Hash> is the mutable
750+
one. Don't get them mixed up! Like C<Map> in JavaScript, C<Map> and C<Hash> can
751+
have any type of key or value, not just strings for keys.
752+
753+
Like C<Map>, C<Set> also has both a mutable and an immutable equivalent in Perl
754+
 6. L<Set|/type/Set> is the immutable one, and L<SetHash|/type/SetHash> is the
755+
mutable one.
615756
616757
=head2 Functions
617758
@@ -625,6 +766,12 @@ say VERSION_NUMBER; # OUTPUT: 33554432
625766
626767
# TBD
627768
769+
=head1 Asynchronous programming
770+
771+
=comment TODO
772+
773+
# TBD
774+
628775
=head1 The networking API
629776
630777
=head2 Net

0 commit comments

Comments
 (0)