Google's Go & Exceptions

John D jdean at googling.com
Fri Jan 29 23:11:53 PST 2010


"Walter Bright" <newshound1 at digitalmars.com> wrote in message 
news:hjnt03$1od9$1 at digitalmars.com...
> dsimcha wrote:
>> I've never understood the recommendation not to put complex logic in 
>> the
>> constructor.
>
> Complex logic is fine, just logic that cannot fail.
>
>> If you need complex logic to establish the class's invariant, or
>> need to initialize complicated immutable data structures, where else 
>> is that logic
>> supposed to go?
>
> For example, allocating resources that might fail, can be done with a 
> separate initialization function:
>
> auto c = new Class;
> c.create();

Commonly called "2 step construction". But you forgot to add the check on 
the return val from c.create(). 





More information about the Digitalmars-d mailing list