For scoping, you can still use C++17 init exprs:

>
> if (Local<...> result; v8::JSON::Parse(...).ToLocal(&result)) {
>     result->Set(...);
> }


and if you really want, you can macroify this, e.g.:

#define IF_IS_LOCAL(EXPR, VAR) if (decltype(EXPR.ToLocalChecked()) VAR;
> EXPR.ToLocal(&VAR))
>
> IF_IS_LOCAL(v8::JSON::Parse(...), result) {

    result->Set(...);

}


I feel your pain about common idioms, and I can see the argument in favour
of a std::optional-style operator bool + operator->, but painful life
experience has taught us that it's better to catch coding errors from not
checking a Maybe type early, and the best way to do that is to force the
API user to explicitly check the validity of the handle -- especially in
cases where IsEmpty is rarely true, so it might sneak through all but the
most exhaustive tests.

If you really really hate it, then rather than patching v8.h I'd suggest
adding a wrapper class around either Local<T> or MaybeLocal<T>, so that you
don't have the sharing code issue.

On Tue, Mar 6, 2018 at 10:23 AM, Zac Hansen <xax...@gmail.com> wrote:

> Well, it's nice to have the scoping of the variable to the if block.
>
> I mean, this is mostly all stylistic, but it would just be nice to not
> have to think "oh, wait, it's v8 so I have to do it this other way" as much
> when there's a standard way to interface with the type of behavior in C++.
>
> My code base uses a bunch of different APIs and having common idioms look
> drastically different between them and standard c++  just makes
> reading/writing code more painful  because you have to remember how v8 does
> things now.. basically forced context switches within a single logical
> construct in my overall code.  It's obviously not the end of the world, but
> the more it looks like normal C++ the more efficient I am.
>
> Does adding the explicit operator bool make things more likely to be
> accidentally skipped over for properly testing return values?   If I can't
> at least get that I'll probably just maintain a patched version of v8.h for
> my own use and that's just annoying and makes it so I can't share any of
> that code with anyone else.
>
>
> --Zac
>
>
>
>
>
> On Tue, Mar 6, 2018 at 2:08 AM, Leszek Swirski <lesz...@chromium.org>
> wrote:
>
>> I think the expected pattern is more along the lines of:
>>
>>>
>>> Local<...> result;
>>> if (v8::JSON::Parse(...).ToLocal(&result)) {
>>>     result->Set(...);
>>> }
>>
>>
>> Would that be succinct enough for you, or is it the lack of type
>> deduction of result that is the problem? That could be avoided with some
>> macro+decltype hackery.
>>
>> - Leszek
>>
>> On Tue, Mar 6, 2018 at 12:11 AM, Zac Hansen <xax...@gmail.com> wrote:
>>
>>> I can see where you are coming from even if I don't agree, but that
>>> doesn't seem to preclude adding the `explicit operator bool`, does it?
>>> Just that alone would get me 95% of what I want.
>>>
>>> I'd suggest adding it to maybe, as well, I think.
>>>
>>> --Zac
>>>
>>>
>>> On Mon, Mar 5, 2018 at 4:03 PM, Jakob Kummerow <jkumme...@chromium.org>
>>> wrote:
>>>
>>>> I think this is mostly because the whole point of MaybeLocal is to be
>>>> explicit. "ToLocalChecked" should say "this can crash if the MaybeLocal is
>>>> empty" loudly in your face, rather than hiding it in a small operator.
>>>>
>>>> There are many ways to make V8's API more concise but less
>>>> safe/obvious. I agree that it is unfortunate how verbose the
>>>> .IsEmpty/.ToLocalChecked pattern is, but we care more about avoiding bugs
>>>> than about avoiding a few extra characters. If you know what you're doing,
>>>> you can create your own helpers and abstractions.
>>>>
>>>>
>>>> On Mon, Mar 5, 2018 at 3:18 PM <xax...@gmail.com> wrote:
>>>>
>>>>> Specifically, I'm suggesting adding:
>>>>>
>>>>> diff --git a/include/v8.h b/include/v8.h
>>>>>
>>>>> index b96771ed45..5d5685839a 100644
>>>>>
>>>>> --- a/include/v8.h
>>>>>
>>>>> +++ b/include/v8.h
>>>>>
>>>>> @@ -383,6 +383,18 @@ class MaybeLocal {
>>>>>
>>>>>      return IsEmpty() ? default_value : Local<S>(val_);
>>>>>
>>>>>    }
>>>>>
>>>>>
>>>>>
>>>>> +  explicit operator bool() const {
>>>>>
>>>>> +      return !this->IsEmpty();
>>>>>
>>>>> +  }
>>>>>
>>>>> +
>>>>>
>>>>> +  Local<T> & operator->() {
>>>>>
>>>>> +      return this->ToLocalChecked();
>>>>>
>>>>> +  }
>>>>>
>>>>> +
>>>>>
>>>>> +  Local<T> operator*() {
>>>>>
>>>>> +      return this->ToLocalChecked();
>>>>>
>>>>> +  }
>>>>>
>>>>> +
>>>>>
>>>>>   private:
>>>>>
>>>>>    T* val_;
>>>>>
>>>>>  };
>>>>>
>>>>> On Monday, March 5, 2018 at 3:02:11 PM UTC-8, xax...@gmail.com wrote:
>>>>>>
>>>>>> I'd really like to be able to say:
>>>>>>
>>>>>> if (auto result = v8::JSON::Parse(...)) {
>>>>>>     (*result)->Set(...);
>>>>>> }
>>>>>>
>>>>>> instead of
>>>>>>
>>>>>> if (auto result =  v8::JSON::Parse(...); !result.IsEmpty()) {
>>>>>>     result.ToLocalChecked()->Set(...);
>>>>>> }
>>>>>>
>>>>>> or worse without '17.
>>>>>>
>>>>>> Is there a technical or policy reason this hasn't/can't be added?
>>>>>> I'd be happy to submit it.
>>>>>>
>>>>>> Thank you.
>>>>>>
>>>>>> --Zac
>>>>>>
>>>>>>
>>>>>> --
>>>>> --
>>>>> v8-dev mailing list
>>>>> v8-dev@googlegroups.com
>>>>> http://groups.google.com/group/v8-dev
>>>>> ---
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "v8-dev" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to v8-dev+unsubscr...@googlegroups.com.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>> --
>>>> --
>>>> v8-dev mailing list
>>>> v8-dev@googlegroups.com
>>>> http://groups.google.com/group/v8-dev
>>>> ---
>>>> You received this message because you are subscribed to a topic in the
>>>> Google Groups "v8-dev" group.
>>>> To unsubscribe from this topic, visit https://groups.google.com/d/to
>>>> pic/v8-dev/sw1rs5U6UNg/unsubscribe.
>>>> To unsubscribe from this group and all its topics, send an email to
>>>> v8-dev+unsubscr...@googlegroups.com.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
>>> --
>>> v8-dev mailing list
>>> v8-dev@googlegroups.com
>>> http://groups.google.com/group/v8-dev
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "v8-dev" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to v8-dev+unsubscr...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
>> --
>> v8-dev mailing list
>> v8-dev@googlegroups.com
>> http://groups.google.com/group/v8-dev
>> ---
>> You received this message because you are subscribed to a topic in the
>> Google Groups "v8-dev" group.
>> To unsubscribe from this topic, visit https://groups.google.com/d/to
>> pic/v8-dev/sw1rs5U6UNg/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> v8-dev+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> --
> v8-dev mailing list
> v8-dev@googlegroups.com
> http://groups.google.com/group/v8-dev
> ---
> You received this message because you are subscribed to the Google Groups
> "v8-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to v8-dev+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
-- 
v8-dev mailing list
v8-dev@googlegroups.com
http://groups.google.com/group/v8-dev
--- 
You received this message because you are subscribed to the Google Groups 
"v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to