Looking for Champion: Null Coalescing (??) and Null Conditional Member Access (?.) operators

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Looking for Champion: Null Coalescing (??) and Null Conditional Member Access (?.) operators

Arash Motamedi
I’d like to propose two new operators that I’ve appreciated using in C#, with appropriate modifications for Ecmascript.

?? Null-Coalescing Operator

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null or undefined; otherwise it returns the right hand operand. (modified excerpt from C# definition, here.)  

Examples:
 
let u = undefined;
let nu = u ?? 0; 
//  nu = (u !== undefined && u !== null) ? u : 0

let n = null;
let nn = n ?? "Default"; 
//  nn = (n !== undefined && n !== null) ? n : "Default";

let p = someObj.someProp ?? "Hello"; 
//  p = (someObj.someProp !== undefined && someObj.someProp !== null) ? someObj.someProp : "Hello";

The ?? operator allows for a very terse syntactic representation of a rather common statement, and its value becomes very clear when evaluating and accessing properties on objects, as illustrated in the 3rd example above. For credit, comparison, and further details, please review the C# null coalescing operator information here


?. Null Conditional Member Access and ?[ Null Conditional Index Access

Used to test for null or undefined before performing a member access (?.) or index (?[) operation. (modified excerpt from C# definition here.) 

Examples:
 
let n = null;
let nn = n?.name; 
//  nn = (n.name !== null && u.name !== undefined) ? u.name : u.name;

let p = {name: "John"};
let l = p.lastName?.length; 
//  l = (u.lastName !== null && u.lastName !== undefined) ? u.lastName.length : u.lastName;

The ?. and ?[ operators allow for graceful access (as opposed to null/undefined reference errors) to object members, and are particularly useful when used in a chained manner. For credit, comparison, and further details, please review the C# null conditional member access operator information here


Combining the above operators can enable very concise syntax for checking against null/undefined and providing default values in a graceful manner. 

let lastNameLength = person.lastName?.length ?? 0; 
let cityToUppercase = person.address?.city?.toUpperCase() ?? "N/A"; 


Looking forward to working with the community and hopefully bringing these two operators to the language. 

Best,
Arash


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Looking for Champion: Null Coalescing (??) and Null Conditional Member Access (?.) operators

Claude Pache
There are already official proposals for those. See:


and search for “Nullish coalescing Operator” for the first of your suggestions and “Optional Chaining” for the second one.

—Claude



Le 20 déc. 2017 à 09:03, Arash Motamedi <[hidden email]> a écrit :

I’d like to propose two new operators that I’ve appreciated using in C#, with appropriate modifications for Ecmascript.

?? Null-Coalescing Operator

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null or undefined; otherwise it returns the right hand operand. (modified excerpt from C# definition, here.)  

Examples:
 
let u = undefined;
let nu = u ?? 0; 
//  nu = (u !== undefined && u !== null) ? u : 0

let n = null;
let nn = n ?? "Default"; 
//  nn = (n !== undefined && n !== null) ? n : "Default";

let p = someObj.someProp ?? "Hello"; 
//  p = (someObj.someProp !== undefined && someObj.someProp !== null) ? someObj.someProp : "Hello";

The ?? operator allows for a very terse syntactic representation of a rather common statement, and its value becomes very clear when evaluating and accessing properties on objects, as illustrated in the 3rd example above. For credit, comparison, and further details, please review the C# null coalescing operator information here


?. Null Conditional Member Access and ?[ Null Conditional Index Access

Used to test for null or undefined before performing a member access (?.) or index (?[) operation. (modified excerpt from C# definition here.) 

Examples:
 
let n = null;
let nn = n?.name; 
//  nn = (n.name !== null && u.name !== undefined) ? u.name : u.name;

let p = {name: "John"};
let l = p.lastName?.length; 
//  l = (u.lastName !== null && u.lastName !== undefined) ? u.lastName.length : u.lastName;

The ?. and ?[ operators allow for graceful access (as opposed to null/undefined reference errors) to object members, and are particularly useful when used in a chained manner. For credit, comparison, and further details, please review the C# null conditional member access operator information here


Combining the above operators can enable very concise syntax for checking against null/undefined and providing default values in a graceful manner. 

let lastNameLength = person.lastName?.length ?? 0; 
let cityToUppercase = person.address?.city?.toUpperCase() ?? "N/A"; 


Looking forward to working with the community and hopefully bringing these two operators to the language. 

Best,
Arash

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Looking for Champion: Null Coalescing (??) and Null Conditional Member Access (?.) operators

Arash Motamedi
Excellent! Thanks so much for the pointers. These will be great syntactic improvements. 

Cheers,
Arash

On Wed, Dec 20, 2017 at 12:12 AM, Claude Pache <[hidden email]> wrote:
There are already official proposals for those. See:


and search for “Nullish coalescing Operator” for the first of your suggestions and “Optional Chaining” for the second one.

—Claude



Le 20 déc. 2017 à 09:03, Arash Motamedi <[hidden email]> a écrit :

I’d like to propose two new operators that I’ve appreciated using in C#, with appropriate modifications for Ecmascript.

?? Null-Coalescing Operator

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null or undefined; otherwise it returns the right hand operand. (modified excerpt from C# definition, here.)  

Examples:
 
let u = undefined;
let nu = u ?? 0; 
//  nu = (u !== undefined && u !== null) ? u : 0

let n = null;
let nn = n ?? "Default"; 
//  nn = (n !== undefined && n !== null) ? n : "Default";

let p = someObj.someProp ?? "Hello"; 
//  p = (someObj.someProp !== undefined && someObj.someProp !== null) ? someObj.someProp : "Hello";

The ?? operator allows for a very terse syntactic representation of a rather common statement, and its value becomes very clear when evaluating and accessing properties on objects, as illustrated in the 3rd example above. For credit, comparison, and further details, please review the C# null coalescing operator information here


?. Null Conditional Member Access and ?[ Null Conditional Index Access

Used to test for null or undefined before performing a member access (?.) or index (?[) operation. (modified excerpt from C# definition here.) 

Examples:
 
let n = null;
let nn = n?.name; 
//  nn = (n.name !== null && u.name !== undefined) ? u.name : u.name;

let p = {name: "John"};
let l = p.lastName?.length; 
//  l = (u.lastName !== null && u.lastName !== undefined) ? u.lastName.length : u.lastName;

The ?. and ?[ operators allow for graceful access (as opposed to null/undefined reference errors) to object members, and are particularly useful when used in a chained manner. For credit, comparison, and further details, please review the C# null conditional member access operator information here


Combining the above operators can enable very concise syntax for checking against null/undefined and providing default values in a graceful manner. 

let lastNameLength = person.lastName?.length ?? 0; 
let cityToUppercase = person.address?.city?.toUpperCase() ?? "N/A"; 


Looking forward to working with the community and hopefully bringing these two operators to the language. 

Best,
Arash

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Looking for Champion: Null Coalescing (??) and Null Conditional Member Access (?.) operators

Ahmad Bamieh
Thank you for this detailed write up! I believe you can help in the existing proposals!

Cheers!
Ahmad Bamieh,

On Wed, Dec 20, 2017 at 10:18 AM, Arash Motamedi <[hidden email]> wrote:
Excellent! Thanks so much for the pointers. These will be great syntactic improvements. 

Cheers,
Arash

On Wed, Dec 20, 2017 at 12:12 AM, Claude Pache <[hidden email]> wrote:
There are already official proposals for those. See:


and search for “Nullish coalescing Operator” for the first of your suggestions and “Optional Chaining” for the second one.

—Claude



Le 20 déc. 2017 à 09:03, Arash Motamedi <[hidden email]> a écrit :

I’d like to propose two new operators that I’ve appreciated using in C#, with appropriate modifications for Ecmascript.

?? Null-Coalescing Operator

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null or undefined; otherwise it returns the right hand operand. (modified excerpt from C# definition, here.)  

Examples:
 
let u = undefined;
let nu = u ?? 0; 
//  nu = (u !== undefined && u !== null) ? u : 0

let n = null;
let nn = n ?? "Default"; 
//  nn = (n !== undefined && n !== null) ? n : "Default";

let p = someObj.someProp ?? "Hello"; 
//  p = (someObj.someProp !== undefined && someObj.someProp !== null) ? someObj.someProp : "Hello";

The ?? operator allows for a very terse syntactic representation of a rather common statement, and its value becomes very clear when evaluating and accessing properties on objects, as illustrated in the 3rd example above. For credit, comparison, and further details, please review the C# null coalescing operator information here


?. Null Conditional Member Access and ?[ Null Conditional Index Access

Used to test for null or undefined before performing a member access (?.) or index (?[) operation. (modified excerpt from C# definition here.) 

Examples:
 
let n = null;
let nn = n?.name; 
//  nn = (n.name !== null && u.name !== undefined) ? u.name : u.name;

let p = {name: "John"};
let l = p.lastName?.length; 
//  l = (u.lastName !== null && u.lastName !== undefined) ? u.lastName.length : u.lastName;

The ?. and ?[ operators allow for graceful access (as opposed to null/undefined reference errors) to object members, and are particularly useful when used in a chained manner. For credit, comparison, and further details, please review the C# null conditional member access operator information here


Combining the above operators can enable very concise syntax for checking against null/undefined and providing default values in a graceful manner. 

let lastNameLength = person.lastName?.length ?? 0; 
let cityToUppercase = person.address?.city?.toUpperCase() ?? "N/A"; 


Looking forward to working with the community and hopefully bringing these two operators to the language. 

Best,
Arash

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss