Hi Liz, thanks for your reply.

Can I call the method from static context?
I mean: MyClass.FOO



> Subject: Re: Constants as members of a class
> From: l...@dijkmat.nl
> Date: Fri, 18 Dec 2015 10:23:11 +0100
> To: perl6-users@perl.org
> 
> 
> > On 18 Dec 2015, at 03:46, TS xx <maringa...@hotmail.com> wrote:
> > 
> > Hello dear perl6 users,
> > 
> > I was in the need of declaring a member variable as a constant integer. 
> > After many syntax tryouts I came to this one:
> > 
> > class MyClass {
> >     has int $.myConst;
> > 
> >     method new () {
> >         return self.bless();
> >     }
> > 
> >     submethod BUILD () {
> >         constant $!myConst = 1;
> >     }
> > 
> >     method showMyConst () {
> >         print $!myConst;
> >     }
> > }
> > 
> > But I'm getting the followinf error message: "Twigil-Variable constants not 
> > yet implemented. Sorry."
> > 
> > The only place in the docs where I have found any reference to constants is 
> > in here: https://doc.perl6.org/language/variables#The_%3F_Twigil
> > But it's not what I am looking for :/
> > 
> > So my questions are:
> > Is the syntax right and the thing isn't implemented yet?
> > Is the syntax (or the full concept) wrong?
> > Do I have and old interpreter (This is perl6 version 2015.02 built on 
> > MoarVM version 2015.02)?
> 
> Yes, that is ancient in rakudo years  :-)
> 
> The question you should ask you: is the constant different for each 
> instantiated object or not.
> 
> If not, and you only need it inside of your class, you can just make it a 
> constant in the mainline of the class:
> 
> class MyClass {
>     constant FOO = 42;
>     …
> }
> 
> If you need to expose a method returning such a constant:
> 
> class MyClass {
>     method FOO { 42 }
>     …
> }
> 
> Otherwise, you probably just need to add an attribute.
> 
> 
> 
> Liz
                                          

Reply via email to