Re: Constants as members of a class

2015-12-18 Thread Elizabeth Mattijsen

> On 18 Dec 2015, at 03:46, TS xx  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

Re: Constants as members of a class

2015-12-18 Thread Moritz Lenz
Hi,

On 12/18/2015 03:46 AM, TS xx wrote:
> Hello dear perl6 users,
> 
> I was in the need of declaring a member variable as a constant integer.

I don't understand this. If it's a constant, why does it need to be
member at all? A member is per-instance storage, which seems to be a
waste for a constant.

Just do

class MyClass {
method TheConstant { 42 }
}

Then you can use it both on the class:

say MyClass.TheConstant

or on an instance:

say MyClass.new.TheConstant


Cheers,
Moritz


RE: Constants as members of a class

2015-12-18 Thread TS xx
Thanks Liz,

I will stick with the method aproach.

Regards,
Emiliano

> Subject: Re: Constants as members of a class
> From: l...@dijkmat.nl
> Date: Fri, 18 Dec 2015 23:49:53 +0100
> To: perl6-users@perl.org
> 
> > On 18 Dec 2015, at 23:37, TS xx <maringa...@hotmail.com> wrote:
> > 
> > Hi Liz, thanks for your reply.
> > 
> > Can I call the method from static context?
> > I mean: MyClass.FOO
> 
> In general, yes:
> 
> class A {
> method FOO { 42 }   # note the method doesn’t reference any attributes
> }
> say A.FOO;
> say A.new.FOO;  # also works on instances
> 
> You can even have a method to be called in case of a static (class) context, 
> and one to be called in the case of an instance:
> 
> class B {
> has $.FOO = 666;
> multi method FOO(B:U:) { 42 } # B is the class, :U is undefined, 
> final : for self
> multi method FOO(B:D:) { $!FOO }  # :D is defined, 
> }
> say B.FOO; # 42
> say B.new.FOO; # 666
> say B.new(FOO => 3.14).FOO;# 3.14
> 
> 
> Hope this helps!
> 
> 
> 
> Liz
  

RE: Constants as members of a class

2015-12-18 Thread TS xx
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
  

RE: Constants as members of a class

2015-12-18 Thread TS xx
Thanks anyway

The new method is there just to state the example.

Date: Thu, 17 Dec 2015 22:58:32 -0500
Subject: Re: Constants as members of a class
From: awwa...@thelackthereof.org
To: maringa...@hotmail.com
CC: perl6-users@perl.org

Two things jump out at me. One is that I think you don't need that "new" 
method. Second -- yes, this is a very old interpreter. I unfortunately don't 
know about the twigil variable constant things.
--Brock

On Thu, Dec 17, 2015 at 9:46 PM, 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)?

Thanks to all.

Kind Regards,
Emiliano
  

  

Re: Constants as members of a class

2015-12-18 Thread Elizabeth Mattijsen
> On 18 Dec 2015, at 23:37, TS xx  wrote:
> 
> Hi Liz, thanks for your reply.
> 
> Can I call the method from static context?
> I mean: MyClass.FOO

In general, yes:

class A {
method FOO { 42 }   # note the method doesn’t reference any attributes
}
say A.FOO;
say A.new.FOO;  # also works on instances

You can even have a method to be called in case of a static (class) context, 
and one to be called in the case of an instance:

class B {
has $.FOO = 666;
multi method FOO(B:U:) { 42 } # B is the class, :U is undefined, final 
: for self
multi method FOO(B:D:) { $!FOO }  # :D is defined, 
}
say B.FOO; # 42
say B.new.FOO; # 666
say B.new(FOO => 3.14).FOO;# 3.14


Hope this helps!



Liz

Re: Constants as members of a class

2015-12-17 Thread Brock Wilcox
Two things jump out at me. One is that I think you don't need that "new"
method. Second -- yes, this is a very old interpreter. I unfortunately
don't know about the twigil variable constant things.

--Brock


On Thu, Dec 17, 2015 at 9:46 PM, TS xx  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)?
>
> Thanks to all.
>
> Kind Regards,
> Emiliano
>


Constants as members of a class

2015-12-17 Thread TS xx
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)?

Thanks to all.

Kind Regards,
Emiliano