Skip to content

Operator precedence not being respected #61922

@lauracallahan-wk

Description

@lauracallahan-wk

General info

  • Dart 2.19.6 (stable) (Tue Mar 28 13:41:04 2023 +0000) on "macos_arm64"
  • on macos / Version 15.6.1 (Build 24G90)
  • locale is en-US
  • Chrome
val: autoDismiss ?? shouldDoSomething ? state.autoDismiss : false

According to the operators documentation, an if-null should be considered higher priority than a ternary operation. However, in our app we were seeing that if autoDismiss was set to true, we were still setting val to false. shouldDoSomething is false in this scenario.

I would have expected this to be read as if autoDismiss is null, then evaluate the ternary, but it seems to be evaluating instead as autoDismiss(true) AND shouldDoSomething(false), then evaluate the ternary or just ignoring the if-null portion.

Adding parentheses resolves the issue and evaluates the way that I would expect.

val: autoDismiss ?? (shouldDoSomething ? state.autoDismiss : false)

Is this expected behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions