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

Error: methods declared outside of class {...} #4976

Open
p6rt opened this issue Jan 2, 2016 · 5 comments
Open

Error: methods declared outside of class {...} #4976

p6rt opened this issue Jan 2, 2016 · 5 comments
Labels
LTA

Comments

@p6rt
Copy link

@p6rt p6rt commented Jan 2, 2016

Migrated from rt.perl.org#127117 (status was 'open')

Searchable as RT127117$

@p6rt
Copy link
Author

@p6rt p6rt commented Jan 2, 2016

From @zoffixznet

If you have a file that declares class Foo { ... } and then you define a method OUTSIDE of those curlies and then proceed to call it from another method declared inside of those curclies, you receive a confusing message that talks about attribute access and no hints that you're calling a method that's outside the class's definition. Attached is a sample class that can be called with `perl6 -I. -MFoo -e 'Foo.new.bar'` to reproduce the error. Which on my machine looks like this​:

zoffix@​ZofMain​:/tmp/tmp.DPywLvk6la$ cat > Foo.pm6
class Foo {
  has @​!bars = <foo bar ber mer>;
  method bar { self.mers; }
}

method mers {
  say @​!bars;
}
zoffix@​ZofMain​:/tmp/tmp.DPywLvk6la$ perl6 -I. -MFoo -e 'Foo.new.bar'
===SORRY!=== Error while compiling /tmp/tmp.DPywLvk6la/Foo.pm6
Cannot understand @​!bars in this context
at /tmp/tmp.DPywLvk6la/Foo.pm6​:7
------> say @​!bars;
===SORRY!=== Error while compiling /tmp/tmp.DPywLvk6la/Foo.pm6
Cannot understand @​!bars in this context
at /tmp/tmp.DPywLvk6la/Foo.pm6​:7
------> say @​!bars;
zoffix@​ZofMain​:/tmp/tmp.DPywLvk6la$ perl6 -v
This is Rakudo version 2015.12-64-g6612747 built on MoarVM version 2015.12
implementing Perl 6.c.
zoffix@​ZofMain​:/tmp/tmp.DPywLvk6la$

@p6rt
Copy link
Author

@p6rt p6rt commented Jan 2, 2016

@p6rt
Copy link
Author

@p6rt p6rt commented Jan 2, 2016

From @niner

Well, declaring methods outside of class scopes is a feature​:

perl6 -e 'class Foo { has $.foo; }; my $bar = my method bar() { say "bar $.foo"; }; Foo.^add_method("bar", $bar); Foo.new(​:foo<foo>).bar;'
bar foo

Of course, as the method is outside the class scope, it cannot access private attributes.
Do you have a suggestion on how we could detect this situation and what the error message should look like?

@p6rt
Copy link
Author

@p6rt p6rt commented Jan 2, 2016

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

@p6rt
Copy link
Author

@p6rt p6rt commented Jan 3, 2016

From @zoffixznet

Well, declaring methods outside of class scopes is a feature​:

Didn't realize it was a feature. My only suggestion would be to change "in this context" to "methods declared outside of class declaration" (or something like that). Not sure how doable that is.

@p6rt p6rt added the LTA label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.