Pharo扩展实现银行转账时触发SubscriptOutOfBounds:5错误求助
SubscriptOutOfBounds:5 in Your Pharo Promises Bank Transfer Implementation Hey there! Let’s dig into that frustrating SubscriptOutOfBounds:5 error you’re hitting. Even if you don’t think you’re using arrays directly, they’re often hiding in plain sight—either in your Promise extension’s internals, or in the bank account logic you built on top of it. Here’s how to track it down:
1. Check Your Promise Extension’s Internal Array Usage
Most Promise implementations rely on arrays under the hood to manage things like callback queues, resolved value lists, or chainable handlers. For example:
- A
callbacksarray storingthen/catchhandlers - A
stateHistoryarray tracking promise lifecycle events - A
resolvedValuesarray for handling promise.all-style logic
If your extension is hardcoding an index (like trying to access callbacks at:5) instead of iterating safely over the array’s actual size, that’s a prime culprit. Look for code like:
"Risky: assumes there are at least 5 callbacks" (callbacks at: 5) value: self resolvedValue
Replace that with safe iteration:
"Safe: iterates only over existing callbacks" callbacks do: [:callback | callback value: self resolvedValue]
2. Audit Hidden Array Use in Your Bank Transfer Logic
Even if you’re using objects like Account or Bank, their internal implementations might use arrays:
- If your
Bankclass stores accounts in an array (instead of a dictionary), accessingaccounts at: targetAccountIdcould fail if the ID doesn’t match a valid array index. - Transaction logs or balance history arrays in your
Accountclass might be getting accessed with an out-of-bounds index during the transfer flow.
For example, if you’re fetching a target account like this:
targetAccount := bank accounts at: transferRequest targetId
If targetId is 5 but the accounts array only has 3 entries, you’ll hit the error immediately. Switching to a dictionary (keyed by account number) would avoid this entirely.
3. Use the Debugger’s Call Stack to Pinpoint the Exact Line
Your debugger is your best friend here. When the error fires:
- Look at the top stack frame to see which method triggered the
SubscriptOutOfBoundserror. It’ll show you exactly which array was accessed, and which index (5) caused the problem. - Check the array’s current size in the debugger’s variables panel—Pharo arrays are 1-indexed, so an array with size 4 can’t be accessed at index 5.
- Trace back through the stack to see how that array was populated. Was it initialized with too few elements? Did a loop increment an index beyond the array’s length?
4. Rule Out Recursion-Related Array Bounds Issues
Since you referenced a recursive method-related SO post, double-check if your Promise extension uses recursion (e.g., chaining promises recursively). If a recursive loop is incrementing an index without checking the array’s bounds, it could easily slip past the array’s length and trigger this error.
内容的提问来源于stack exchange,提问作者Gakuo

