user-documentation icon indicating copy to clipboard operation
user-documentation copied to clipboard

Mention postfix and prefix on increment / decrement guide

Open AndrewDiMola opened this issue 3 years ago • 1 comments

Please complete the information below:

Where is the problem?

Incrementing / Decrementing guide.

What is the problem?

We never talk of the consequences of a post- or pre-increment/decrement.

If Hack is someone's first language (maybe one day), they should also learn what this means conceptually.


Please don't change anything below this point.


  • Build ID: HHVM=HHVM-4.154.0:HSL=v4.108.1:2022-05-16T17:51:53+0000:1fa47f258c6b68f8ec01899aa82fd6ffa0957109
  • Page requested: /hack/expressions-and-operators/incrementing-and-decrementing
  • Page requested at: Wed, 18 May 2022 21:13:52 +0000
  • Controller: GuidePageController

AndrewDiMola avatar May 18 '22 21:05 AndrewDiMola

Looking into this!

thesourabh avatar Aug 09 '22 06:08 thesourabh

As I understand it, there is no significant difference in Hack: in other languages, it matters when it is used in an expression, but ++$x and $x++ are both statements in Hack, not expressions, so the difference can't come up in valid hack code:

$x = 123;
var_dump($x++); // Parsing[1002] Assignments can no longer be used as expressions.
var_dump(++$x); // Parsing[1002] Assignments can no longer be used as expressions.

fredemmott avatar Sep 07 '22 21:09 fredemmott

@fredemmott

I ran some test code and I think it makes a difference with this specific example, but I can't think of any other reason why since you can't use them for assignments to other variables or such.

EvanHelstrom avatar Sep 07 '22 21:09 EvanHelstrom

@EvanHelstrom the runtime still understands it (it wasn't always banned), but as the typechecker bans it, it's not considered valid Hack code - especially as it's 1002 'parse error', which no projects should permit HH_FIXMEs for.

fredemmott avatar Sep 07 '22 21:09 fredemmott

I guess we can just close this issue then since there isn't really a need for it to be adjusted. Or should it be explained with no use case?

EvanHelstrom avatar Sep 07 '22 21:09 EvanHelstrom

If there's no real consequence in Hack and you think readers will know ++ and -- by proxy of other languages, I'm fine with you closing this issue.

AndrewDiMola avatar Sep 07 '22 21:09 AndrewDiMola

Per discussion above, there's no significant difference as this syntax is only allowed in statements.

Wilfred avatar Sep 16 '22 00:09 Wilfred