On Mon, Aug 3, 2015 at 6:03 PM, Mike Hommey <m...@glandium.org> wrote: > On Mon, Aug 03, 2015 at 05:55:01PM -0700, Jonas Sicking wrote: >> On Mon, Aug 3, 2015 at 5:06 PM, Karl Tomlinson <mozn...@karlt.net> wrote: >> > but I don't see much advantage of public constructors over >> > something like >> > >> > static already_AddRefed<Foo> Foo::Create() >> > >> > or >> > >> > static nsRefPtr<Foo> Foo::Make() >> >> Fair enough. Though it is somewhat more boilerplate in the class >> itself. But maybe not enough to be a concern. >> >> > Constructors also have the potentially awkward feature of never >> > failing, and hence the abundance of separate Init() methods. >> >> Technically you can make the ctor take an nsresult out argument. I >> think we have a few classes that do that. > > That's quite horrible to read and write, though.
How would you make a factory function like the above fail? Returning an nsresult will make it not much better than NS_NewFoo() pattern. Returning null won't let you indicate a useful error code (though maybe there's no such thing as useful error codes and we should just report errors to the console). The only other alternative is to use the nsresult out argument, which I agree is not easy on the eyes. / Jonas _______________________________________________ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform