-
Notifications
You must be signed in to change notification settings - Fork 601
Add __CLASS__ keyword
#21306
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
Add __CLASS__ keyword
#21306
Conversation
|
Hrm; everything passes except the probably because the newly-added opchecker function isn't done right, somehow. Hrm. |
|
I can reproduce this locally, and it is fixed by simply moving the |
|
g++ now fixed. Turns out it needed some extra poking in |
14b5eb1 to
0593b62
Compare
| if(!CvIsMETHOD(PL_compcv)) | ||
| croak("Cannot use __CLASS__ outside of a method or field initializer expression"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pardon my ignorance 😃 I think ADJUST is currently considered a method, but eventually will become a phaser. It is intended that __CLASS__ will work there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah definitely. The ADJUST blocks will become individual blocks that are all still part of one overall containing method, so __CLASS__ still still work fine there. It'll just stop being one method per block.
| class WithCustomField { | ||
| use constant DEFAULT_X => 10; | ||
| field $x = __CLASS__->DEFAULT_X; | ||
| } | ||
|
|
||
| This allows subclasses to override the method with different behaviour. | ||
|
|
||
| class DifferentCustomField :isa(WithCustomField) { | ||
| sub DEFAULT_X { rand > 0.5 ? 20 : 30 } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initial design of Corinna was to separate methods and subroutines. Thus, you can't call subs like you can call methods. If we had the :common attribute, this could be written as:
class WithCustomField {
method DEFAULT_X :common {10}
field $x = __CLASS__->DEFAULT_X;
}This allows subclasses to override the method with different behaviour.
class DifferentCustomField :isa(WithCustomField) {
method DEFAULT_X :common { rand > 0.5 ? 20 : 30 }
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed so. This is the best we can write currently, but once we have common methods we can change the documentation to fit the better design.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should add a note about this in the documentation? If people start writing code relying on this, future releases will break it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's all very new and experimental now anyway. Any of the docs should be read in that light anyway, so I don't think adding specific wording to basically every claim and sugestion is going to be all that helpful.
| { | ||
| class Test1 { | ||
| method hello { return "hello, world"; } | ||
|
|
||
| method classname { return __CLASS__; } | ||
| } | ||
|
|
||
| my $obj = Test1->new; | ||
| isa_ok($obj, "Test1", '$obj'); | ||
|
|
||
| is($obj->hello, "hello, world", '$obj->hello'); | ||
|
|
||
| is($obj->classname, "Test1", '$obj->classname yields __CLASS__'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a test verifying that __CLASS__ is available in ADJUST blocks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added, re-testing now.
Also a couple of small neatenings of code/test; kept as separate commits in case of subsequent revert