I'm testing Perl6 on OS X with Rakudo Star 2011-04, Parrot v3.3.0
The id is Int and I put a Str => no error! Same for the username. How to have strong typing?#!/usr/bin/perl6 use v6; class User is rw { has Int $.id; has Str $.username; } my $Paul = User.new; $Paul.id = "sa"; $Paul.username = 12; say $Paul.id; say $Paul.username;
Also I know for public access attributes, perl6 automatically generate setters and getters. When I try to create a method called username, I have the error : "A method named 'username' already exists in class 'User'. It may have been supplied by a role."
I would like to create my own setter for username so that I can forbid usernames such as 'admin' or 'root'. Or I could put a 'when' on the attribute (like parameters on functions). That would do something like this (doesn't work) :
class User is rw { has Int $.id; has Str $.username when not /admin|root/; }
In reply to Perl6: Class attribute strict static type by dièze
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |