Skip to content

Commit 6268c1f

Browse files
committed
Adds explanations on native types refs #1512
1 parent 0e6e6d2 commit 6268c1f

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

doc/Language/nativetypes.pod6

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
55
=SUBTITLE Using the types the compiler and hardware make available to you.
66
7+
X<|int>X<|uint>X<|num>X<|str>
8+
=head1 Types with native representation
9+
710
Some simple types in Perl 6 have a I<native> representation, indicating that they will use the C language representation provided by the compiler, operating system and machine. These are the four native types available:
811
9-
X<|int>X<|uint>X<|num>X<|str>
1012
=begin table
1113
int Equivalent to Int
1214
uint Equivalent to Int with the unsigned trait
@@ -19,20 +21,57 @@ L<NativeCall|/lang/nativecall> interface (e.g., Perl 6's C<int> can be 8 bytes
1921
but C's C<int> is only 4 bytes). Instead, the types below will have to be used
2022
instead of the types C<int> or C<num> listed above.
2123
22-
X<|int8>X<|int16>X<|int32>X<|int64>X<|uint8>X<|uint16>X<|uint32>X<|uint64>X<|num32>X<|num64>
24+
In general, these variables will behave in the same way as regular scalar
25+
variables, in a behavior that is called I<auto-boxing>; however, there are some
26+
differences, since what you are actually declaring is how they will be
27+
represented, not their actual type. The first one is that their type will be
28+
actually their equivalent type, not their native type.
29+
30+
my int $intillo = 3;
31+
say $intillo.^name; # OUTPUT: «Int␤»
32+
33+
This obviously means that they will smartmatch their equivalent, not their
34+
native type:
35+
36+
my str $strillo = "tres";
37+
say $strillo ~~ str; # OUTPUT: «False␤»
38+
say $strillo ~~ Str; # OUTPUT: «True␤»
39+
40+
They cannot be bound either. Trying to do C<my num $numillo := 3.5> will raise
41+
the exception C<Cannot bind to natively typed variable '$variable-name'; use
42+
assignment instead>.
43+
44+
X<|int8>X<|int16>X<|int32>X<|int64>X<|uint8>X<|uint16>X<|uint32>X<|uint64>X<|num32>X<|num64>X<|byte>
45+
=head1 Types with native representation and size
46+
47+
What has been mentioned about types with native representation also applies
48+
here; they will be auto-boxed to Perl 6 types and will not be boundable.
49+
However, these types, which are listed in the table below, have the
50+
characteristic of being usable in L<NativeCall> functions.
51+
2352
=begin table
2453
int8 (int8_t in C, also used for char)
2554
int16 (int16_t in C, also used for short)
2655
int32 (int32_t in C, also used for int)
2756
int64 (int64_t in C)
28-
uint8 (uint8_t in C, also used for unsigned char)
57+
byte, uint8 (uint8_t in C, also used for unsigned char)
2958
uint16 (uint16_t in C, also used for unsigned short)
3059
uint32 (uint32_t in C, also used for unsigned int)
3160
uint64 (uint64_t in C)
3261
num32 (float in C)
3362
num64 (double in C)
3463
=end table
3564
65+
These types have a fixed size representation which is independent of the
66+
platform, and thus can be used safely for those calls. Nothing prevents us from
67+
using it in any other environment, if we so wish. In the same way as the types
68+
above, this size will have to be taken into account when assigning values to
69+
variables of this type:
70+
71+
my byte $intillo = 257;
72+
say $intillo; # OUTPUT: «1␤»
73+
74+
Since C<byte> is able to hold only 8 bits, it will I<wrap over> and assign the result of the original value modulo 256, which is what is shown.
3675
3776
=end pod
3877

0 commit comments

Comments
 (0)