This is an automated email from the ASF dual-hosted git repository. joshtynjala pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/royale-docs.git
commit 9265524f6bbf0d8cdbef59523226f67f6dc9973b Author: Josh Tynjala <joshtynj...@apache.org> AuthorDate: Thu Aug 21 14:01:57 2025 -0700 null-conditional-operator and nullish-coalescing-operator --- features/as3.md | 2 + features/as3/null-conditional-operator.md | 51 ++++++++++++++++++++++++ features/as3/nullish-coalescing-operator.md | 62 +++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) diff --git a/features/as3.md b/features/as3.md index 68c4b3d..8069e0c 100644 --- a/features/as3.md +++ b/features/as3.md @@ -66,6 +66,8 @@ The following new ActionScript features are available with the Royale compiler: * [Abstract Classes](features/as3/abstract-classes) * [Import Aliases](features/as3/import-aliases) +* [Null Conditional Operator](features/as3/null-conditional-operator) +* [Nullish Coalescing Operator](features/as3/nullish-coalescing-operator) * [Private Constructors](features/as3/private-constructors) * [Type Inference](features/as3/type-inference) * [Verbatim Strings](features/as3/verbatim-strings) diff --git a/features/as3/null-conditional-operator.md b/features/as3/null-conditional-operator.md new file mode 100644 index 0000000..f661a8e --- /dev/null +++ b/features/as3/null-conditional-operator.md @@ -0,0 +1,51 @@ +--- +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +layout: docpage +title: Null conditional operator +description: Null conditional operator in ActionScript +permalink: /features/as3/null-conditional-operator +--- + +# Null conditional operator in ActionScript + +[Apache Royale](https://royale.apache.org/){:target='\_blank'} adds support for the _null conditional operator_ in [ActionScript](features/as3), which uses the symbol `?.`. The expression `a?.b` works similarly to a member access expression, like `a.b`. The difference when using `?.` is that, if the left operand is _nullish_, it will immediately return `null` instead of trying to access the right operand and throwing an exception. + +The null conditional operator is also supported by the compiler included with the Adobe AIR SDK starting with version 50.0. + +## Code example + +Consider the following code that uses the `?.` operator to access the field `a` on the condition that the object is not nullish. + +```as3 +var obj:Object = null; +var resolved:Object = obj?.a; +trace(resolved); // null +``` + +If the expression `obj.a` were used instead of `obj?.a`, then an exception would be thrown instead. + +If the object is not nullish, then it works similarly to regular member access. + +```as3 +var obj:Object = {a: 123.4}; +var resolved:Object = obj?.a; +trace(resolved); // 123.4 +``` + +## Limitations of the null conditional operator in Royale + +Other ActionScript compilers, such as the one in the [Apache Flex SDK](https://flex.apache.org/){:target='_blank'}, may not recognize the null conditional operator. Attemping to pass ActionScript or MXML source code that contains null conditional operators to another compiler may result in compile-time errors. In other words, to write 100% portable ActionScript code that works with any compiler, avoid using null conditional operator and any of Royale's other [extensions to the ActionScri [...] diff --git a/features/as3/nullish-coalescing-operator.md b/features/as3/nullish-coalescing-operator.md new file mode 100644 index 0000000..8f0fc0c --- /dev/null +++ b/features/as3/nullish-coalescing-operator.md @@ -0,0 +1,62 @@ +--- +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +layout: docpage +title: Nullish coalescing operator +description: Nullish coalescing operator in ActionScript +permalink: /features/as3/nullish-coalescing-operator +--- + +# Nullish coalescing operator in ActionScript + +[Apache Royale](https://royale.apache.org/){:target='\_blank'} adds support for the _nullish coalescing operator_ in [ActionScript](features/as3), which uses the symbol `??`. The `??` operator accepts two operands, one each on its left and right sides. Which operand is returned depends on whether the left operand is _nullish_ or not. + +Nullish values include `null` and `undefined` only. If the left operand is not nullish, then it is returned immediately, without executing the right operand. If the left operand is nullish, then the right operand is returned instead. + +The nullish coalescing operator is also supported by the compiler included with the Adobe AIR SDK starting with version 50.0. + +## Code example + +Consider the following code that uses the `??` operator to provide a non-null value if the left operand is nullish. + +```as3 +var obj:Object = null; +trace(obj == null); // true +var resolved:Object = obj ?? {}; +trace(obj == null); // false +``` + +The next code shows that the left operand will be returned when it is not nullish. + +```as3 +var obj:Object = {}; +var resolved:Object = obj ?? {}; +trace(resolved == obj); // true +``` + +The code below demonstrates the difference between `??` and `||` when encountering a _falsy_ value. + +```as3 +var num:Number = 0; +var checkNullish:Number = num ?? -1; +var checkFalsy:Number = num || -1; +trace(checkNullish == -1); // false +trace(checkFalsy == -1); // true +``` + +## Limitations of the nullish coalescing operator in Royale + +Other ActionScript compilers, such as the one in the [Apache Flex SDK](https://flex.apache.org/){:target='_blank'}, may not recognize the nullish coalescing operator. Attemping to pass ActionScript or MXML source code that contains nullish coalescing operators to another compiler may result in compile-time errors. In other words, to write 100% portable ActionScript code that works with any compiler, avoid using nullish coalescing operator and any of Royale's other [extensions to the Acti [...]