Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

(Perl6) Signatures

by LanX (Saint)
on Aug 08, 2017 at 13:11 UTC ( [id://1196984]=perlquestion: print w/replies, xml ) Need Help??

LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I picked some examples from https://perl6advent.wordpress.com/2013/12/21/day-21-signatures/ but am not sure if they are still valid syntax, because of confusion with Synopsies S06

named parameters

call syntax
sub doctor(:$number, :$prop) { say "Doctor # $number liked his $prop"; } doctor(:prop("cricket bat"), :number<5>);

Could this call also be written as

doctor( prop => "cricket bat" , number => 5)

?

internal names

But when defining another internal name it shouldn't be possible to write

sub doctor(:$prop => $internal) { ... , right?

rather

sub doctor(:$prop($internal) ) { ... , right?

mandatory named arguments

Talking about mandatory named parameters where do I place the ! then?

sub doctor(:$prop! => $internal)

resp

sub doctor(:$prop($internal)! ) { ... , right?

or

sub doctor(:$prop!($internal) ) { ... , right?

==== default values

And when defining a default value, where do I place it?

sub doctor(:$prop($internal) = 'something ) { ... ' , right?

==== why optional inverted default for named

BTW positional arguments are by default mandatory and become optional when a default value is provided.

But named arguments are by default optional, why not the same trick?

where constraints

use v6; multi odd-or-even(Int $i where * %% 2) { say "even" }; multi odd-or-even(Int $i) { say "odd"};

apparently means something like sub { $_ % 2 } .

why two %% and is it possible to use a more familiar P5 syntax here? like where {$x % 2} ?

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re: (Perl6) Signatures
by moritz (Cardinal) on Aug 08, 2017 at 13:54 UTC
    Could this call also be written as
    doctor( prop => "cricket bat" , number => 5)

    Yes!

    Regarding internal names: The proper way to write it is

    sub f(:prop($internal)) { ... }

    If want an alias, you can put another colon before the dollor:

    sub f(:prop(:$alias)) { ... }

    Easy rule: the dollar goes on the variable, the colon before a public name.

    Re default values: correct.

    BTW positional arguments are by default mandatory and become optional when a default value is provided. But named arguments are by default optional, why not the same trick?

    I wasn't there for the decision, so I can only speculate, but maybe it's in analogy to options for command-line applications, which tend to be optional by default as well.

    apparently means something like sub { $_ % 2 } . why two %% and is it possible to use a more familiar P5 syntax here? like where {$x % 2} ?

    %% is the "divisible by" operator. You could write it as { $_ %% 2 } or { $_ % 2 == 0 } instead.

Re: (Perl6) Signatures
by rafaelschipiura (Initiate) on Aug 08, 2017 at 14:25 UTC

    :prop("cricket bat") :number<5> and prop => "cricket bat" , number => 5 are equivalent, yes.

    > :prop("cricket bat") === prop => "cricket bat" True
    But it doesn't work for the alias syntax because that syntax for creating an alias and not the pair constructor. They just look similar.

    sub doctor(:$prop($internal) ) { This is incorrect syntax. It should be: sub doctor(:prop(:$internal) ) { Please note the lack of $, which makes it just an alias, not a Pair.

    So, regarding mandatory parameters, neither of your examples will work: sub doctor(:$prop($internal)! ) { sub doctor(:$prop!($internal) ) { You need: sub doctor(:prop($internal)! ) { or sub doctor(:$prop(:$internal)! ) {

    > But named arguments are by default optional, why not the same trick?

    It's all about what looks better. According to TimToady. There's no real answer to this question. In this case he is copying how it works in the Unix command line.

    > %%

    Means "divisible by". It's true when the left argument is divisible by the right one. False otherwise. sub { $_ % 2 } is the opposite, and is numeric instead of boolean like the other one.

    $a %% $b is more like !($a % $b). Note the cast to Boolean and the logical negation.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1196984]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found