Skip to content

Instantly share code, notes, and snippets.

@LLFourn
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LLFourn/2d1b407f06c486a12173 to your computer and use it in GitHub Desktop.
Save LLFourn/2d1b407f06c486a12173 to your computer and use it in GitHub Desktop.

In this particular case you don't even need a custom accessor. You can create extra constraints on your base type constraint with a where clause:

class Wizzard {
    has Int $.mana is rw where * > 0;
}

But if you did want to create a custom accessor you simply have to define a method with the same name and add the is rw trait.

method mana is rw {
   say "running out of mana" if $!mana < 20;
   $!mana;
}

You will still be able to use equals assignment syntax like $wizard.mana = 50 but you will not be able to capture the value before it is assigned. If you want something even more fine grained explain Proxy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment