![]() |
|
XP is just a number | |
PerlMonks |
Re: Thoughts on new 'class' OO in upcoming perlby haj (Vicar) |
on Mar 06, 2023 at 15:30 UTC ( [id://11150793]=note: print w/replies, xml ) | Need Help?? |
I have been playing with the new object system and in particular with its playground Object::Pad for some time now, and I'd like to encourage you to do this as well. For many of your points here there are solutions or workarounds. Whether you like them or not is a matter of personal taste, but knowing them helps. Well, and some of your points are just plain wrong. Abort object creation?In the new object system it is possible to abort object creation, and in many cases the OO system will do it by itself. Most of that has already been implemented in and popularized by Moose. The idea to have a declaration of constructor parameters instead of writing code for them. I agree that this needs effort to learn the new declarative syntax, and there are subtle differences between the different OO systems. In your example, a mandatory constructor parameter is missing, and in this case, and the OO system can abort the object creation. The difference is that the OO systems don't return undef (as your example does) but throw an exception. Of course, if you prefer to return undef in that case, you can have that, too: All you need to know is that stuff you used to write in the body of new now goes into the ADJUST phaser. Constructor not proper functions?With the new Object system a new function will be generated for you. But that's all about it. Put simply, the difference is that in the new OO systems new is sort of a bless on steroids, so instead of calling bless in the constructor function of your choice, you now call new. So, the only downside is that the name new is now occupied by the OO system itself. I am using "named" constructors a lot (they now call Class->new(...) instead of bless(...)), occasionally multiple constructors and also factories. In your factory, you need to change the name to e.g. urlHandler, since new is now taken. That doesn't seem unsurmountable. Deciding when to call parent constructor?Ah, here's an area where there are differences between bless-based classes, Moo(se)-style OO systems and the new core OO system. In short: You don't ever call parent constructors in core OO. This is intentional. Instead, the declarations of the parent classes are evaluated by the auto-generated new-method, and the ADJUST-block of the parent is called, in addition to performing these steps for the current class. Your example of a subclass setting a field to a fixed value has been discussed in great length in the GitHub discussions on Corinna, in particular issue 77. The length of this discussion should indicate that people are aware that this is a bit of a rough area, but nothing (as someone said on IRC) that an ADJUST block can't fix. ConclusionI agree that the core OO system takes away the flexibility of the DIY-objects one can build with bless. However, ignoring Moo* is, well, ignorance. Many lessons from Moo* went into core OO. It is a trade-off between the benefits and restrictions, but you can't get a good view if you ignore the benefits. The best thing of all: bless isn't going to go away. You can continue to hand-craft your classes, hand-write constructors and accessors for maximum flexibility.
In Section
Meditations
|
|