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

Inheriting from lexical classes seems borked #6251

Closed
p6rt opened this issue May 18, 2017 · 6 comments
Closed

Inheriting from lexical classes seems borked #6251

p6rt opened this issue May 18, 2017 · 6 comments

Comments

@p6rt
Copy link

p6rt commented May 18, 2017

Migrated from rt.perl.org#131324 (status was 'rejected')

Searchable as RT131324$

@p6rt
Copy link
Author

p6rt commented May 18, 2017

From @lizmat

Should this work? If not, why not? What would be the way to inherit from lexical classes in other compunits?

./A.pm6​:
my class A {}

./B.pm6​:
my class B {
  use A;
  also is A;
}

$ PERL6LIB=. 6 'use B'
===SORRY!=== Error while compiling /Users/liz/Github/rakudo.moar/B.pm6 (B)
Invalid typename 'A'
at /Users/liz/Github/rakudo.moar/B.pm6 (B)​:4
------> also does A⏏;

@p6rt
Copy link
Author

p6rt commented May 18, 2017

From @zoffixznet

To the best of my knowledge​:

On Thu, 18 May 2017 03​:52​:42 -0700, elizabeth wrote​:

Should this work? If not, why not?

No, because the symbol is lexical to the compunit. It's the same as how you won't be able
to access subs or `my` constants or variables, unless you make them `our` or export them.

What would be the way to inherit from lexical classes in other compunits?

One way would be to export them​:

./A.pm6​:
my class A is export {}

./B.pm6​:
my class B {
  use A;
  also is A;
}
say B.^mro;

$ ./perl6 -I. -MB -e ''
((B) (A) (Any) (Mu))

You can also use export sub​:
./A.pm6​:
sub EXPORT {
  Map.new​: 'A' => my class A {}
}

Or (I'm unsure what the end goal here is), take a special export arg and only export the class if it's present​:

./A.pm6​:
sub EXPORT ($secret-password?) {
  Map.new​: (
  'A' => my class A {
  } if $secret-password ~~ 42
  )
}

./B.pm6​:
my class B {
  use A;
  also is A;
}
say B.^mro;

$ ./perl6 -I. -MB -e ''
===SORRY!=== Error while compiling /home/zoffix/CPANPRC/rakudo/B.pm6 (B)
'B' cannot inherit from 'A' because it is unknown.
at /home/zoffix/CPANPRC/rakudo/B.pm6 (B)​:3

Now change ./B.pm6 to​:
my class B {
  use A 42;
  also is A;
}
say B.^mro;

$ ./perl6 -I. -MB -e ''
((B) (A) (Any) (Mu))

@p6rt
Copy link
Author

p6rt commented May 18, 2017

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented May 19, 2017

From @LLFourn

FWIW theoretically you get at any 'my' symbol in another module through the
compunit interface by getting the CompUnit​::Handle and using the '.unit'
method to get the top lexpad of the loaded module.

On Fri, May 19, 2017 at 8​:20 AM Zoffix Znet via RT <
perl6-bugs-followup@​perl.org> wrote​:

To the best of my knowledge​:

On Thu, 18 May 2017 03​:52​:42 -0700, elizabeth wrote​:

Should this work? If not, why not?

No, because the symbol is lexical to the compunit. It's the same as how
you won't be able
to access subs or `my` constants or variables, unless you make them `our`
or export them.

What would be the way to inherit from lexical classes in other compunits?

One way would be to export them​:

./A.pm6​:
my class A is export {}

./B.pm6​:
my class B {
use A;
also is A;
}
say B.^mro;

$ ./perl6 -I. -MB -e ''
((B) (A) (Any) (Mu))

You can also use export sub​:
./A.pm6​:
sub EXPORT {
Map.new​: 'A' => my class A {}
}

Or (I'm unsure what the end goal here is), take a special export arg and
only export the class if it's present​:

./A.pm6​:
sub EXPORT ($secret-password?) {
Map.new​: (
'A' => my class A {
} if $secret-password ~~ 42
)
}

./B.pm6​:
my class B {
use A;
also is A;
}
say B.^mro;

$ ./perl6 -I. -MB -e ''
===SORRY!=== Error while compiling /home/zoffix/CPANPRC/rakudo/B.pm6 (B)
'B' cannot inherit from 'A' because it is unknown.
at /home/zoffix/CPANPRC/rakudo/B.pm6 (B)​:3

Now change ./B.pm6 to​:
my class B {
use A 42;
also is A;
}
say B.^mro;

$ ./perl6 -I. -MB -e ''
((B) (A) (Any) (Mu))

@p6rt
Copy link
Author

p6rt commented Jul 7, 2017

From @jnthn

On Thu, 18 May 2017 15​:19​:26 -0700, cpan@​zoffix.com wrote​:

To the best of my knowledge​:

On Thu, 18 May 2017 03​:52​:42 -0700, elizabeth wrote​:

Should this work? If not, why not?

No, because the symbol is lexical to the compunit. It's the same as
how you won't be able
to access subs or `my` constants or variables, unless you make them
`our` or export them.

This and the rest of the response are correct; ticket rejected as there's no bug here. The point of lexical stuff in modules is that it's not visible outside unless explicitly exported.

@p6rt
Copy link
Author

p6rt commented Jul 7, 2017

@jnthn - Status changed from 'open' to 'rejected'

@p6rt p6rt closed this as completed Jul 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant