-
Notifications
You must be signed in to change notification settings - Fork 19
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
Classes system feedback #103
Comments
use 5.038;
use feature 'class';
no warnings 'experimental';
class ParentClass{
field $myParam :param(param) = undef;
ADJUST {
$myParam //= $self->initParam();
}
method initParam {
die "Subclasses are supposed to override this";
}
method myParam { $myParam }
}
class ChildClass :isa(ParentClass) {
method initParam () {
return "someFixedValue";
}
}
say ChildClass->new->myParam; This has a quirk: I need a public method initParam which is not supposed to be part of the class API. I guess some future version of Corinna will improve on that, as several declarative ways have been discussed, Alas, it's not in Perl 5.38 (and not in Object::Pad either).
...Perhaps I should add some background, too: I'm one of the maintainers of Perl 5 support for Emacs, which as of now covers feature 'class'. |
So there is a problem with: class ParentClass{
private final String myParam;
public ParentClass(String param){
myParam = param;
}
}
class ChildClass extends ParentClass{
public ChildClass(){
super("someFixedValue");
}
} The problem is that you can't use ChildClass in all places ParentClass is used. Specifically you have to re-write any constructors to no-longer take class ParentClass {
field $param :param;
method param() { $param }
}
class ChildClass {
field $parent = ParentClass->new(param => "someFixedValue");
method param() { $parent->param() }
} In my experience anything sufficiently complicated with inheritance becomes a pain in the ass. Most often I'm gonna want to subclass ChildClass but with a different value of "someFixedValue" … or I'm going to want an instance of ChildClass but I need a different value for "someFixedValue" but the implementation of ChildClass is tightly bound to that value always being "someFixedValue", so I have to entirely re-implement ChildClass with that new value. If you want some kind of contractual constraint, eventually we'll have Roles that you can use to define an interface that requires Regarding 5, in addition to what @HaraldJoerg said … I disagree that Inheritance is a "thing we almost always have".1 I'd rather it be a thing we almost never have. I'm not sure I've yet found a place where I preferred it over role composition or even better, simple delegation (like above). (This is despite my having argued for it already in other tickets/discussions in this same repo.) I strongly suspect it was a violation of Ovid's Rule of Parsimony to even have it at all in the current code but it's a feature that's so deeply ingrained into Perl's OO culture that I doubt we could get away without it. Finally 6, … this goes back to the same problems you were having with 1 I think. Perl injects a constructor implicitly, you never actually define one. That said eventually we will have static methods in classes that you can do something like
Which is basically how you'd implement the same idiom in Moose or Moo. Footnotes
|
Some background:
So my feedback is kinda view from the outside and it based on current implementation in 5.38, not all propositions in this particular repo (sorry, did not have an opportunity to read all of them).
I wrote some of my concerns to the p5p and was suggested to submit them here, so - here I am.
So here are my 5 cents:
package
)I understand historical reasons and syntax continuity, but feels like modern stuff should be modern and easy to use, and less follow historical syntax rules without a really good reason for language users.
7. I'd like to be able to declare multiple fields with one instruction:
Again, sorry if this was already discussed/described in this repo or p5p.
The text was updated successfully, but these errors were encountered: