Closed LeXofLeviafan closed 4 years ago
Seems like a reasonable thing to want. Can I assume your desired behavior if it
is nullish is to return undefined
—i.e., have x?{foo}
be equivalent to that{foo} if x?
?
The logical thing here would likely be to do the same as when accessing a single field ((?.foo)
), which does indeed behave as you described (though technically the single-field example compiles to it != null ? it.foo : void 8
).
The single-field case is different in that it doesn't create a wrapper object. So maybe x?{foo}
should be equivalent to that{foo} if x?
, but the alternative would be maybe it's equivalent to { foo: x?foo }
; I could see both being doing ‘the same’ as the single-field case, the only difference being whether one conceptually applies the slice desugaring first or the existence desugaring first.
Still think the that{foo} if x?
proposal is the most logical thing?
Producing a single undefined
is more easily manageable (and makes more sense in general case) than producing a structure filled with undefined
values (and is equivalent to maybe-monad behaviour), so yes, short-circuiting at the initial point sounds more reasonable to me.
If you want to have alternative behaviour (object filled with undefined
values) available, it could be done with a different syntax (i.e. ?
or ?.
short-circuits, and .?
produces a slice: order defines behaviour), though I doubt you'll find a lot of uses for such output.
Agreed. If there's anyone out there who wants objects full of undefined
, they can ask for that later.
Actually, on further investigation, this is a regression that I introduced; the 1.5.0 release of LiveScript did exactly this. I'll fix it and add a test.
All of these
produce exactly the same output:
Similarly, every single one of these
also produce identical output:
The expected behaviour here, naturally, would be to make an additional nil check when
?
is used (or at least produce a compile error if such syntax is not supported).