4

Nan 2.0, for compatibility with Node 4.0, introduced Maybe and MaybeLocal types, and several functions that return them. However, most of them do not seem to accept Maybe objects, and in several cases I would want to compose those methods. For example (assuming I have a function that returns a MaybeLocal<String>), I would like to do return Nan::To<String>(Nan::Get(object, key)) instead of

Nan::MaybeLocal<Value> maybe_value = Nan::Get(object, key);
if (maybe_value.IsEmpty()) {
  return Nan::Nothing;
}
return Nan::To<String>(maybe_value.ToLocalChecked());

Is there a reasonable way to do this without writing my own wrapper around every one of these functions?

ZachB
  • 13,051
  • 4
  • 61
  • 89
murgatroid99
  • 19,007
  • 10
  • 60
  • 95
  • I thought that just returning `maybe_value.ToLocalChecked()` would do this. Since the `MaybeLocal` template will return `Nothing` in the empty instance, and a `String` in the filled instance, do you need to handle all the casting yourself, or are you just overkilling it? – Christopher Karper Sep 19 '15 at 02:26
  • `maybe_value` has type `MaybeLocal`, so `maybe_value.ToLocalChecked()` has type `Local`. – murgatroid99 Sep 19 '15 at 02:49
  • What would the return type of this function be? What allows NaN::Nothing and Local? – Christopher Karper Sep 21 '15 at 19:50
  • "assuming I have a function that returns a `MaybeLocal`". `Nan::To` has return type `MaybeLocal`. – murgatroid99 Sep 21 '15 at 19:52
  • Net result, this function returns a ```MaybeLocal``` that could potentially be empty? ```Nan::To(Nan::Get(object, key));``` would do the exact same thing, right? ```Nan::To<>()``` returns a ```Maybe<>``` for cases like this, where conversion fails. – Christopher Karper Sep 21 '15 at 20:00
  • The whole point of this question is that `Nan::Get` returns a `MaybeLocal` and the argument to `Nan::To` is a `Local`, so you can't do `Nan::To(Nan::Get(object, key));` – murgatroid99 Sep 21 '15 at 20:03
  • I apologize for being slow on the uptake, but there's something that just doesn't make sense to me here. This may convert the underlying type from ```v8::Value``` to ```v8::String```, but still leaves the function consumer having to check for Empty value. What's the value added? I'm still learning Nan, so I'm no expert, just trying to help out. – Christopher Karper Sep 21 '15 at 20:17
  • The point here is not to expose the code I wrote as an entire function. The point is that it takes 5 lines to express something that should reasonably be expressible in one line, and I'm wondering if there's a better way to do it than what I have. – murgatroid99 Sep 21 '15 at 20:26

0 Answers0