Difference between Chaining operator(&&) and Literal Operator(&)
Recently I talked about the Chaining Operator(&&) and how it simplifies dealing with texts and strings. I wanted to talk about a similar-looking operator with some similar functions to the chaining operator which makes it easy to confuse each other.
A literal operator(&) is used to join two or more literals to produce a single literal. However, there is a hard character length limit of 255.
Example of using a literal operator:
There are more features and limitations to a literal operator. Below are the points on which it's similar/different to a Chaining operator.
- Similar to the chaining operator, you can use join string templates using &.
- Any trailing blank in text field literals is preserved, unlike chaining operator.
- Can span multiline:
- Calculated at compile-time.
- Can not have more than 255 characters(See the error in the above image), if
you want to have a longer character,
use the chaining operator.
- Can not mix literal types while joining them using the literal operator.
- Can not use variables at operand positions
As you can see, the literal operator has a specific function i.e. to join literals and support multiline declarations while easy to confuse with the chaining operator.