• dev_null@lemmy.ml
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      2 years ago

      Because it’s not one. Ternary operator is A ? B : C, Elvis operator is A ?: B. The same two characters are involved, but both the syntax and effect is different.

      • AVincentInSpace@pawb.socialBanned from community
        link
        fedilink
        English
        arrow-up
        2
        ·
        2 years ago

        The second one isn’t valid syntax in any programming language I’m familiar with. What does it do?

        • dev_null@lemmy.ml
          link
          fedilink
          arrow-up
          4
          ·
          2 years ago

          It’s a shorthand for writing this:

          variable = if (input != null) input else default
          

          This is equivalent:

          variable = input ?: default