Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat($parse): allow for assignments in ternary operator branches #5434

Closed
wants to merge 1 commit into from

Conversation

rodyhaddad
Copy link
Contributor

Currently in angular expressions, the ternary operator is defined as:

TernaryOperatorExpression :
    LogicalORExpression ? LogicalORExpression : LogicalORExpression

but in javascript, it's

TernaryOperatorExpression :
    LogicalORExpression ? AssignmentExpression : AssignmentExpression

So I thought it would be good for ternary operator in ng-expression to behave the same way as in js.


Given the following expression

1 ? a : b = null

currently it's evaluated as

(1 ? a : b) = null

which throws an error

It will now be evaluated as

1 ? a : (b = null)

CLA signed as Rodric Haddad

@noducks
Copy link

noducks commented Feb 20, 2014

Very useful. Thanks.

@IgorMinar
Copy link
Contributor

Sorry Rody but this feature would encourage people to put more logic into the template that belongs to JavaScript.

Templates in Angular are already very rich, but we always try to find the balance between usability and maintainability. You should be able to express yourself but not at the cost of maintainability. I think this feature would cause more problems than benefits.

@IgorMinar IgorMinar closed this Apr 4, 2014
@rodyhaddad
Copy link
Contributor Author

I'll try to explain my reasoning a bit more:

We decided to add ternary operator support back in v1.1.5.
All languages that support ternary operator, support assignments in their branches.
Even the AngularDart Parser does! :-)

This could be seen as a fix() instead of a feat(), plus it's a trivial change.

It's really assignments that can cause problems in templates. But they're are allowed, and won't be removed any time soon as they're convenient for users.


I'm not asking for this to be merged now. It's probably never needed in the wild.

The discovery of this bug came as I was trying to write a formal definition of angular expressions (partly because of #3869), and it seemed wrong to disallow the following in a language:

(condition === true)
  ? num1 = 1
  : num2 = 2

but to allow the following

num3 = 3

@IgorMinar
Copy link
Contributor

The behavior in AngularDart is wrong. We are going to change it: dart-archive/angular.dart#870

Allowing assignments makes many things, including dirty checking much harder because an expression that was expected to be idempotent would now have side-effects which we are not ready to handle.

@IgorMinar
Copy link
Contributor

Wait. What?

Did you post a reply here by accident when you meant to reply to the
related issue in AngularDart?
On Apr 9, 2014 8:20 PM, "Miško Hevery" notifications@github.com wrote:

Dirty checking already does not allow assignments, and such feature will
not be added. You will get an error if you try to do that.

Assignments are allowed in non-watch expressions such as ng-click Your
fix looks right, so I am going to merge it in.

You never created a PR, which is why it took so long for me to get to
this. Create a PR next time.


Reply to this email directly or view it on GitHubhttps://github.com//pull/5434#issuecomment-40039429
.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants