Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot refer to a constant declared in a class in Rakudo #1374

Closed
p6rt opened this issue Oct 22, 2009 · 7 comments
Closed

Cannot refer to a constant declared in a class in Rakudo #1374

p6rt opened this issue Oct 22, 2009 · 7 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Oct 22, 2009

Migrated from rt.perl.org#69967 (status was 'resolved')

Searchable as RT69967$

@p6rt
Copy link
Author

p6rt commented Oct 22, 2009

From @masak

In Rakudo 4c248e5c, constants can no longer be used inside of classes.

$ perl6 -e'class A { constant B = 5; has $!x = B }; A.new'
Null PMC access in isa()
[...]
$ perl6 -e'class A { constant B = 5; has $!x = A​::B }; A.new'
Null PMC access in isa()
[...]

I'm pretty sure this used to work before jnthn++'s merge. Which means
we probably don't have a spectest for this.

Sadly, it's not possible to refer to constants-defined-outside-a-class
from within the class either.

$ perl6 -e 'constant B = 5; class A { has $!x = B }; A.new'
Lexical 'B' not found

Here's a clean workaround, for those who want something like constants
in a class​:

$ perl6 -e 'class A { sub B { 5 }; has $.x = B }; A.new.x.say'
5

@p6rt
Copy link
Author

p6rt commented Oct 22, 2009

From @masak

On Thu Oct 22 04​:05​:08 2009, masak wrote​:

In Rakudo 4c248e5c, constants can no longer be used inside of classes.

$ perl6 -e'class A { constant B = 5; has $!x = B }; A.new'
Null PMC access in isa()
[...]
$ perl6 -e'class A { constant B = 5; has $!x = A​::B }; A.new'
Null PMC access in isa()
[...]

I'm pretty sure this used to work before jnthn++'s merge. Which means
we probably don't have a spectest for this.

Sadly, it's not possible to refer to constants-defined-outside-a-class
from within the class either.

$ perl6 -e 'constant B = 5; class A { has $!x = B }; A.new'
Lexical 'B' not found

Here's a clean workaround, for those who want something like constants
in a class​:

$ perl6 -e 'class A { sub B { 5 }; has $.x = B }; A.new.x.say'
5

<jnthn> Out of curiosity, does a lexical instead of a constant work?
<jnthn> How about an our-scoped variable?
* masak tries
<masak> jnthn​: 'my' doesn't, 'our' works.
<masak> 'my' produces the same error as 'constant'. I'll add that to the ticket.
<jnthn> masak​: OK. And since constants are package scoped, that probably gives us a
pointer.
<masak> nod.
<jnthn> oh, my doesn't work?
<jnthn> Hmm
<jnthn> oh wait
<jnthn> I think constants became our scoped in the spec *after* I implemented them. :-/
<jnthn> So they're probably still lexical.
<masak> aye.
<masak> that is consistent with the data.
<jnthn> *nod*
<jnthn> OK, so it looks like something to do with lexicals and attr initializers.
<jnthn> Maybe we attach the initializer blocks to the wrong lexical scope.

@p6rt
Copy link
Author

p6rt commented Oct 22, 2009

From [Unknown Contact. See original ticket]

On Thu Oct 22 04​:05​:08 2009, masak wrote​:

In Rakudo 4c248e5c, constants can no longer be used inside of classes.

$ perl6 -e'class A { constant B = 5; has $!x = B }; A.new'
Null PMC access in isa()
[...]
$ perl6 -e'class A { constant B = 5; has $!x = A​::B }; A.new'
Null PMC access in isa()
[...]

I'm pretty sure this used to work before jnthn++'s merge. Which means
we probably don't have a spectest for this.

Sadly, it's not possible to refer to constants-defined-outside-a-class
from within the class either.

$ perl6 -e 'constant B = 5; class A { has $!x = B }; A.new'
Lexical 'B' not found

Here's a clean workaround, for those who want something like constants
in a class​:

$ perl6 -e 'class A { sub B { 5 }; has $.x = B }; A.new.x.say'
5

<jnthn> Out of curiosity, does a lexical instead of a constant work?
<jnthn> How about an our-scoped variable?
* masak tries
<masak> jnthn​: 'my' doesn't, 'our' works.
<masak> 'my' produces the same error as 'constant'. I'll add that to the ticket.
<jnthn> masak​: OK. And since constants are package scoped, that probably gives us a
pointer.
<masak> nod.
<jnthn> oh, my doesn't work?
<jnthn> Hmm
<jnthn> oh wait
<jnthn> I think constants became our scoped in the spec *after* I implemented them. :-/
<jnthn> So they're probably still lexical.
<masak> aye.
<masak> that is consistent with the data.
<jnthn> *nod*
<jnthn> OK, so it looks like something to do with lexicals and attr initializers.
<jnthn> Maybe we attach the initializer blocks to the wrong lexical scope.

@p6rt
Copy link
Author

p6rt commented Oct 22, 2009

@masak - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Oct 22, 2009

From @kyleha

This is an automatically generated mail to inform you that tests are now available in t/spec/S04-declarations/constant.t

commit 4384f9e3c5d22e1bbdf564c98773a2b1538e23eb
Author​: moritz <moritz@​c213334d-75ef-0310-aa23-eaa082d1ae64>
Date​: Thu Oct 22 19​:48​:21 2009 +0000

  [t/spec] tests for RT #​69967, constant declarations and classes
 
  git-svn-id​: http://svn.pugscode.org/pugs@&#8203;28884 c213334d-75ef-0310-aa23-eaa082d1ae64

Inline Patch
diff --git a/t/spec/S04-declarations/constant.t b/t/spec/S04-declarations/constant.t
index b207465..e7aa000 100644
--- a/t/spec/S04-declarations/constant.t
+++ b/t/spec/S04-declarations/constant.t
@@ -246,6 +246,21 @@ plan *;
              'assign constant its own value from expression';
 }
 
+#?rakudo skip 'RT 69967'
+{
+    constant C = 6;
+    class A {
+        constant B = 5;
+        has $.x = B;
+        has $.y = A::B;
+        has $.z = C;
+    }
+
+    is A.new.x, 5, 'Can declare and use a constant in a class';
+    is A.new.y, 5, 'Can declare and use a constant with FQN in a class';
+    is A.new.z, 6, 'Can use outer constants in a class';
+}
+
 done_testing;
 
 # vim: ft=perl6

@p6rt
Copy link
Author

p6rt commented Sep 11, 2011

From @bbkr

Mentioned test is already unfudged and pass on NOM.

@p6rt
Copy link
Author

p6rt commented Sep 11, 2011

@bbkr - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Sep 11, 2011
@p6rt p6rt added the Bug label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant