From 74308fb8e1bf6036214e3c9409261b94384c2f01 Mon Sep 17 00:00:00 2001 From: Lexidor Digital <31805625+lexidor@users.noreply.github.com> Date: Sat, 21 Feb 2026 11:56:11 +0000 Subject: [PATCH] Fix stack-like behavior in 46-generic-types.md Seealso: https://github.com/hhvm/user-documentation/issues/1171 Seealso: https://github.com/hhvm/user-documentation/pull/1183 --- manual/hack/10-types/46-generic-types.md | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/manual/hack/10-types/46-generic-types.md b/manual/hack/10-types/46-generic-types.md index f1766163c..7f20fde3d 100644 --- a/manual/hack/10-types/46-generic-types.md +++ b/manual/hack/10-types/46-generic-types.md @@ -9,23 +9,24 @@ Consider the following example in which `Stack` is a generic class having one ty class StackUnderflowException extends Exception {} class Stack { - private vec $stack; - private int $stackPtr; - - public function __construct() { - $this->stackPtr = 0; - $this->stack = vec[]; - } + private vec $stack = vec[]; + private int $stackPtr = -1; public function push(T $value): void { - $this->stack[] = $value; $this->stackPtr++; + if (C\count($this->stack) === $this->stackPtr) { + $this->stack[] = $value; + } else { + $this->stack[$this->stackPtr] = $value; + } } public function pop(): T { - if ($this->stackPtr > 0) { + if ($this->stackPtr >= 0) { + $element = $this->stack[$this->stackPtr]; + $this->stack[$this->stackPtr] = $this->stack[0]; $this->stackPtr--; - return $this->stack[$this->stackPtr]; + return $element; } else { throw new StackUnderflowException(); }