dièze has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm testing Perl6 on OS X with Rakudo Star 2011-04, Parrot v3.3.0

#!/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;
The id is Int and I put a Str => no error! Same for the username. How to have strong typing?

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/; }

Replies are listed 'Best First'.
Re: Perl6: Class attribute strict static type
by moritz (Cardinal) on Jun 05, 2011 at 07:10 UTC

    Type-checking of attributes is not yet implemented in Rakudo. There's a branch of Rakudo that uses a new object model, which will make it very simple to implement attribute type checking. Expect it to land in the next two or three months.

    Once that's done, you can say

    subset Username of Str where none <admin root>; class User is rw { has Int $.id; has Username $.username; }
      Ok, thanks moritz.