in reply to Re: Override new in Moose for flyweight objects
in thread Override new in Moose for flyweight objects
I'd argue that "new", especially in perl, is that factory. That being said, I don't have a problem with renaming it, but I'm not sure there's a better name. At one point, I had the same issue, just without Moose partly because it didn't exist at the time, where I wanted to load different objects (from disk, but the source didn't really matter), of a limited number of types - so I'd have Product->create("productName") which would load the product if it isn't already, store it in the cache, and return the cached version. (And similar for other types, using a common loader in a common base class.)
But, as a general rule, I see new as a factory that generates the required objects. Which does make it marginally different from (and significantly more useful than) the new keyword in other languages. Conceptually, I'm asking for an object. But the fact that, under the covers, magic is done, usually doesn't matter. If I'm asking for an immutable object, then the fact it gets reused on each call to new is totally and utterly immaterial. And because of this, I can also just re-use it in other places that don't know that this is reusing objects from a cache, that expect to call new and get objects back. Because they call new and get objects back. It duck types perfectly. What more do I need? But if I rename it, then I lose the duck typing, and someone calling new getting new objects for a class where that is the wrong thing means that it no longer duck types properly, and that is its own host of problems.
I'm not saying it's perfect, but, in my experience, it solved more problems to leave new duck typing what I mean than to rename it. In the above case, I ended up having create just call new, or maybe the other way around, I don't remember now, just to get the duck typing working, and things were much simpler after that. After all, that's why I used perl - to make my life easier. If I wanted hard, I'd use Java. :)
|Replies are listed 'Best First'.|
Re^3: Override new in Moose for flyweight objects
by tobyink (Canon) on Apr 08, 2020 at 03:23 UTC