Does Perl 6 have named tuples?

There are various ways of getting something similar.

  • 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
    

There are probably quite a few more that I haven't thought of yet. The real question is how you are going to use it in the rest of your code.


Enums 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␤»

The type that is declared with enum can be used just 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


Looks like the type in Perl 6 that you are looking for is a hash.

See the relevant documentation:

  • Syntax: "Hash literals"
  • Hash

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

my %twostraws = name => 'twostraws', password => 'fr0st1es';

print %twostraws<name>; # twostraws

Tags:

Raku