If we are going to leave bool behind, I think we should use something
Rustic:

template <typename T, typename E>
class MOZ_MUST_USE_TYPE Result : public mozilla::Variant<T, E>
{
  public:
    // Helper methods here...

    explicit operator bool () { return is<T>(); }

    // Etc...
};


Or, if we don't want to boil too many oceans and have something closer to
our usage of bool and not worry about wrapping types:

class Result
{
  public:
    enum class Type : uint32_t
    {
        Ok,
        Oom,
        Exception,
        // Etc...
    };

  private:
    Type type_;

  public:
    // Helper methods here...

    explicit operator bool () { return type_ == Type::Ok;}

    // Etc...
};


On Mon, May 9, 2016 at 4:32 PM, Nicholas Nethercote <[email protected]>
wrote:

> On Sat, Apr 23, 2016 at 9:29 AM, Nicholas Nethercote
> <[email protected]> wrote:
> >
> > one thing I've been thinking about is using
> > MOZ_WARN_UNUSED_RESULT more.
> > It isn't useful for the "return null on failure" case, because the
> > result will always be used even if you forget the check. But it is
> > useful for the "return false on failure" case. I count 113 uses of it
> > currently in js/src/, the majority of which are for functions
> > returning bool. I suspect the number could/should be at least 10x
> > higher than that.
> >
> > I would be happy to try to add more uses of it if people think it's
> worthwhile.
>
> I've started doing this in bug 1267551. (Taking over from bug 606349,
> where this idea was first suggested.)
>
> Having added quite a few MOZ_MUST_USE annotations now I can't help but
> wonder if it would be better to have some kind of result type
> (JSResult?), as an alternative to bool. A bit like nsresult but
> without the zillion different failure values.
>
> With that in place, bool would be used for the return type of
> functions to indicate true/false or yes/no, and JSResult would be used
> in fallible functions to indicate success/failure. And then we could
> use MOZ_MUST_USE_TYPE on JSResult, which would give us assurance (via
> static analysis) that all functions that return this type are checked,
> and then we wouldn't need all these MOZ_MUST_USE annotations
> everywhere.
>
> (We might also need a JSResultUnchecked variant, because for some
> fallible functions not checking the return value is reasonable.)
>
> Nick
> _______________________________________________
> dev-tech-js-engine-internals mailing list
> [email protected]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine-internals
>
_______________________________________________
dev-tech-js-engine-internals mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-internals

Reply via email to