Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring "native" types in Perl 6

In the natives.pm6 module, many native types are declared this way:

my native int is repr('P6int') is Int { }

You can apparently use it in the same way,

my native smallish is repr('P6int') is Int is nativesize(8) { };
say smallish.^mro; # OUTPUT: «((smallish) (Int) (Cool) (Any) (Mu))␤»

Apparently, you need native to specify a representation, since that will fail if nativeis not used. Same with is repr, so they go hand in hand. However, this is just a behavioral observation.

However, I haven't found any documentation of when and where to use native, other than those examples in the implementation. Any help will be appreciated.

like image 923
jjmerelo Avatar asked Jul 04 '18 16:07

jjmerelo


1 Answers

The native package declarator is a Rakudo Perl 6 compiler implementation detail rather than part of the Perl 6 language. Some mechanism was needed to express the relationship between the language runtime and types like int32 and num64 declared in the Perl 6 standard library. A different type of meta-object was needed for natives, and since package declarators (like class and role) work by being mapped to a particular type of meta-object, introducing a native package declarator for this purpose was a neat way to bootstrap that bit of the Perl 6 type system.

Use of native outside of the standard library was never intended (which I can say with confidence, because I invented the mechanism). Given the performance sensitivity of native types, Perl 6 compiler developers should likely be given maximum freedom to choose how they define them, and to be able to refine that over time. There's no certainty that the native declarator will exist forevermore in Rakudo, nor that any future Perl 6 compiler developers will choose to do things in the same way.

If wanting an alias to a native type name, just use constant:

constant smallish = int;

Since constant is evaluated at compile time, this symbol can then be used just like any other type. There's no functionality otherwise available through native that isn't already exposed by types available in Perl 6 or the NativeCall library.

like image 192
Jonathan Worthington Avatar answered Oct 20 '22 19:10

Jonathan Worthington