Pearl 6 called tuples?

I know that Swift called tuples:

let twostraws = (name: "twostraws", password: "fr0st1es") 

so i can say:

 print(twostraws.name) # twostraws 

but in Perl 6 I would say:

 my $list = (twostraws, fr0st1es); say $list[0]; 

Which is not as scary as Swift, so I want to know if there are tuple names in Perl 6?

+6
source share
4 answers

There are various ways to get something like this.

  • Simple hash (recommended)

     my \twostraws = %( 'name' => 'twostraws', 'password' => 'fr0st1es' ); print twostraws<name>; # twostraws{ qw'name' } 
  • List with two methods mixed in

     my \twostraws = ( 'twostraws', 'fr0st1es' ) but role { method name () { self[0] } method password () { self[1] } } put twostraws.name; # `put` is like `print` except it adds a newline 
  • Anonymous class

     my \twostraws = class :: { has ($.name, $.password) }.new( :name('twostraws'), :password('fr0st1es') ) say twostraws.name; # `say` is like `put` but calls the `.gist` method 

Most likely, there are several more that I have not thought about. The real question is how are you going to use it in the rest of your code.

+10
source

Enumerations can have value types that are not Int. You declare them as a list of pairs.

 enum Twostraws (name => "twostraws", password => "fr0st1es"); say name; # OUTPUT«twostraws␀» say password; # OUTPUT«fr0st1es␀» say name ~~ Twostraws, password ~~ Twostraws; # OUTPUT«TrueTrue␀» say name.key, ' ', name.value; # OUTPUT«name twostraws␀» 

A type declared with enum can be used like any other type.

 sub picky(Twostraws $p){ dd $p }; picky(password); # OUTPUT«Twostraws::password␀» 

Edit: see https://github.com/perl6/roast/blob/master/S12-enums/non-int.t

+5
source

It seems that the type of Perl 6 you are looking for is a hash.

See related documentation:

Here is a Perl 6 example that should be equivalent to your Swift example:

 my %twostraws = name => 'twostraws', password => 'fr0st1es'; print %twostraws<name>; # twostraws 
+4
source

the equivalent of perl6 is Pair , and its constructor statement => . They are immutable - after creation, the key and value cannot be changed;

 $ perl6 > my $destination = "Name" => "Sydney" ; Name => Sydney > say $destination.WHAT ; (Pair) > $destination.value = "London"; Cannot modify an immutable Str in block <unit> at <unknown file> line 1 > 

Like the bold comma from perl5, the constructor does not require the left side to be quoted if it has one identifier. There is an alternative syntax for expressing pairs called "colon". You can collect several Pear togeather into a list, but they will only be available to positionaly;

 > $destination = ( Name => "Sydney" , :Direction("East") , :Miles(420) ); (Name => Sydney Direction => East Miles => 420) > say $destination.WHAT ; (List) > say $destination[1] ; Direction => East > 

There are convenient colon syntax options — if the value is a string, you can replace the brackets with brackets and discard the quotation marks. If the value is an integer, you can list the key immediately after the value without quotes. If the value is logical, you can list only the key, if the value is True or a prefix ! if the value is False.

Finally, you can assign several of them to a hash, where access to these values ​​will be possible using the key and changed;

 > my %destination = ( :Name<Sydney> , :Direction<East> , :420miles , :!visited ) ; Direction => East, Name => Sydney, miles => 420, visited => False > say %destination<miles> ; 420 > %destination<visited> = True ; True > 
+3
source

All Articles