-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Different default values for different undefined levels #16
Comments
That seems quite complex for me. The goal of this proposal is to have simple, easy to understand semantics. The only real complexity is the short-circuiting behaviour. But let see how to handle your use cases, using the current
const result = props?.user?.friends?.[0]?.friends ?? defaultValue
For this one, the simplest replacement I can think of is: let tmp = props
const result = tmp == null ? defaultValue : tmp?.user?.friends?.[0]?.friends I think that we should stick if possible with the “left-to-right evaluation order plus short-circuiting” principle if possible. For example, let say that const result = (props ?? defaultValue!).user?.friends?.[0]?.friends
This one is more cumbersome; I wonder however whether that situation happens often. const result = (() => {
let tmp = props
if (tmp == null)
return defaultValue
let tmp = tmp?.user?.friends?.[0]
if (tmp == null)
return undefined
return tmp?.friends
if (tmp == null)
return defaultValue
return tmp
})() With the const result = ((props ?? defaultValue!).user?.friends?.[0] ?? undefined!)?.friends ?? defaultValue But it is not clear to me whether your use cases occurs sufficiently often in real life to balances the burden to learn yet another syntax. Do other languages have solutions for your use cases? |
Thanks to your suggestion. Because I want to restrict the scope of my work, I won’t include it. More specifically:
Of course, further improvements may be considered later if there are real needs, although I don’t expect to personally pursue the work in that direction. If you have any suggestion, you should make your case on the es-discuss mailing list. |
Please see the discussion in facebook/idx#5 for the reason about why we need different default values for different undefined levels.
In this example:
We have 5 chances to get
undefined
:We need some symbol to define the default values for different
undefined
levels.?(...)
will provide a default value if the evaluation returnsundefined
?>
will try to look up a default value in the optional chain?(...)!
will provide a default value only for this evaluation (it will not be looked up for?>
)?
will not try use a default valueFor example:
We provide a
defaultValue
toprops
if it isundefined
. If any following evaluation returnsundefined
and it use?>
, it will look up the default value in the chain.If we use
?(...)!
, for example:the
defaultValue
can't be used in other optional levels (only for its level):We could provide different default values for the chain:
We could use just
?
to not use a default valueHow about this proposal?
The text was updated successfully, but these errors were encountered: