Can I create an Apex.Stack for any type?

As far as I know, there is no such class in the platform's API, look in your own code base for a class called Apex that has an inner class called Stack.

If you want to support other types, you will have to add additional methods for those and generalize the implementation. Other languages include better mechanisms for this situation - see e.g. Java's Generic Types.

Though as this Apex.Stack code probably does very little, changing the method signatures and implementation to use Object rather than Id would probably be the best way to go as then all types would be supported. But casts would be needed when values are popped.

PS

Correction from sfdcfox:

There is a hidden class called Apex.Stack, and a related exception, Apex.StackEmptyException. It has a few methods available in it. It was never documented because of some "edge cases" where it would inexplicably break as well as a general lack of performance. I wasn't given any more than that, but this information came directly from someone with access to the source code for it, so it is at least accurate.


The Apex.Stack class has known limitations and bugs, which is why it was never formally released in the documentation. Do not use it if you care about the stability of your code.

Tags:

Apex