• spongebue@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    11 hours ago

    x = -i;

    Do many languages let you do that? When it’s in front of a variable I would’ve expected it to be a subtraction operator only and you would need to do x = -1 * i;

    • squaresinger@lemmy.world
      link
      fedilink
      arrow-up
      2
      arrow-down
      2
      ·
      4 hours ago

      Why would they not let you do that? I honestly don’t know a single language that wouldn’t let you do that. Same as basic math notation allows you to do that.

      x = -i

      is a totally valid mathematical equation.

    • EvilHankVenture@lemmy.world
      link
      fedilink
      arrow-up
      5
      ·
      10 hours ago

      In most languages I’ve seen - is both a unary negation operator and a subtraction operator depending on context. So it would negate an integer literal or a variable in this context.

    • boonhet@sopuli.xyz
      link
      fedilink
      arrow-up
      2
      ·
      11 hours ago

      Personally I would expect it to behave the same in front of a numeric literal and in front of a variable. I do think most languages do that, but I haven’t actually tested that many and could br wrong.