|Syntactic Confectionery Delight|
Re^3: Moose: return null objectby stvn (Monsignor)
|on Apr 19, 2011 at 02:10 UTC||Need Help??|
So all that's fine, just it seems (very) silly (as in: pointlessly silly, silly oversight kind of silly) that you cannot return undef from a "new" call, and thot perhaps I'd missed something. But I'm not here to pick on Moose either ;)
Well, basically it is not an oversight at all, it is *very* deliberate. This is basically a variant of the "return value" vs. "exception" debate which happens any time you lock a C programmer and Java programmer together in a room for a long enough period of time. We (myself and the other core Moose devs) believe that exceptions are the way to go, so therefore Moose reflects this opinion. But it also goes a little deeper then that, to the philosophy of what is a "Class".
In non-Moose Perl OO, new was simply a method that you could make do whatever you wanted, convention said it was the method that constructed a new instance, but that was simply just convention. There was nothing special about new either, in fact it was a common idiom for a while to do $instance->new as a sort of cloning technique. But in the end, a "Class" in non-Moose Perl OO is not really a rigorously defined concept, but instead really just a loose set of conventions accumulated over time and borrowed from other languages. Now, there is nothing at all wrong with this, it is very powerful and extremely flexible, however it also suffers from the common "dark-side" of TIMTOWTDI.
In contrast, Moose defines "Class" much more rigorously, in particular it thinks that one of the primary functions of a "Class" is as an instance construction factory. So much so that new in Moose will eventually call $class->meta->new_object to get the instance (delegating up to the Class meta-object). So keeping with this philosophy, new should *always* return an instance, and if it cannot for whatever reason return an instance, it should explode loudly with an exception.