On Thursday, April 21, 2016 at 4:33:40 PM UTC+10, Nicholas Nethercote wrote:
> On Thu, Apr 21, 2016 at 4:19 PM, Xidorn Quan <quanx...@gmail.com> wrote:
> >>
> >> Maybe you're referring to factory methods, like this:
> >>
> >>   static T* T::New();
> >>
> >> which would return null on failure. Such methods can be useful, but
> >> there's two problems. First, they're not applicable to stack-allocated
> >> objects. Second, you still have to do your fallible initialization
> >> *within* the factory method, and so you still have to choose with
> >> either constructor+Init or constructor+outparam, so you still have to
> >> make a choice.
> >
> > You can probably merge Init into this factory method, and make constructors
> > private.
> 
> Inlining Init into the factory method doesn't change the fundamentals.
> Initialization is still split across two functions. You still can't
> use references and |const| as much.
> 
> > For stack-allocated objects, you can probably return a Maybe<>?
> 
> I played around with Maybe<> and couldn't come up with something nice.
> I may have missed something. Even if there is a nice way involving
> Maybe<>, I'm pretty sure you'll still end up having to make a choice
> between constructor+Init and constructor+outparam within the factory
> method.
> 
> Nick

How about another generic helper, e.g.:
  template <typename T, typename ...Args>
  Maybe<T> MakeCheckedMaybe(Args&&... aArgs)
  {
    nsresult rv;
    Maybe<T> m = Some(T(std::forward<Args>(aArgs)..., &rv));
    if (NS_SUCCEEDED(rv)) {
      return m;
    }
    return Nothing();
  }

No need for factory methods. ;-)
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to