develooper Front page | perl.perl6.users | Postings from November 2022

Re: What does this line mean?

Thread Previous | Thread Next
Bruce Gray
November 26, 2022 05:06
Re: What does this line mean?
Message ID:

> On Nov 25, 2022, at 9:21 PM, ToddAndMargo via perl6-users <> wrote:
> Hi All,
> I am confused!

The documentation at would certainly benefit from example code!
No examples in the t/ directory use the `blob-from-pointer` sub.

The main doc for NativeCall,
, *does* contain a single example, but it uses a too-clever-to-be-helpful shortcut for a named argument, so I will rephrase it as:
    my $esponja = blob-from-pointer( $inter, elems => 2, type => Blob[int8] );
    my $esponja = blob-from-pointer( $inter, :elems(2), :type(Blob[int8]) );

> sub blob-from-pointer(Pointer:D \ptr, Int :$elems!, Blob:U :$type = Buf) is export {
> What is `Pointer:D \ptr`?
> Why the `\`?

Defining a variable with a backslash-where-the-sigil-should-go creates a "sigilless variable":
The original coder's choice to use this should not be relevant to the caller, and the distinction is not mentioned in the docs at ,
which document the sub as:
    sub blob-from-pointer(Pointer:D, Int :$elems!, Blob:U :$type = Buf)
You can ignore the confusing backslash; just be sure to pass an defined object of type Pointer (or a sub-class of Pointer).

> What is `:$elems!`?
> Why the `:`?
> Why the `!`?

The colon is the standard way to define a *named* parameter.
Named params default to being optional, and the exclamation point changes the parameter to be required.
This is an unusual choice for the API (since it could have been specified as 3 positional parameters), but this API forces more "clues to the reader" to be embedded in each call, providing more clarity in a problem domain prone to pitfalls.

> What is `Blob:U :$type = Buf`
> What does `Blob:U` mean?

:D means "Must be defined"
:U means "Must be undefined"
:_ means "can be defined or undefined" and is the default.

SomeObjectType:U means that the parameter must be undefined, and be of the type SomeObjectType, or a *subclass* of SomeObjectType.
That part of the API looks *very* weird (Why pass a undefined value???), until we observe that Blob and Buf have *sized* subclasses via parameterized role!

> What does `:type` mean?

You are missing the `$` in that question.
`:$type` means that the caller can pass this datum in a named argument style, like `type => SomeTypename` or `:type(SomeTypename)`,
and the called sub will receive the argument in the `$type` variable.
In other words, it is a *named* parameter like the mandatory `:$elems` above, but `:$type` is optional.

> Is `$type` being assigned the type of `Buf`

The equals-sign in a Parameter list defines a default value. `$type` gets assigned the undefined-but-still-useful type-object of `Buf` only if no `type` argument is passed.

So, a real-life call might look like:
	my $q = blob-from-pointer( $p, elems => 10, type => Blob[uint8] );

> :'(
> Yours in confusion,
> -T

Hope this helps,
Bruce Gray (Util of PerlMonks)

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About