I’m at a loss here, what is wrong with this code? ...
# codereview
o
I’m at a loss here, what is wrong with this code? what’s wrong with the Stack I’ve implemented ? https://paste.ofcode.org/MmxwVcvXvdBY5EZfZpTPWS the moment I use Java’s own Stack, the function works fine and returns true for 1st test and false for 2nd
ah damn it, it’s the implementation of
count
, in my code I have
override val count = storage.size
and in the interface it’s with a custom getter
t
You might want to take a look at *<https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-array-deque/%7Ckotlin.collections.ArrayDeque&lt;E&gt;&gt;* If use use addFirst/removeFirst/peekFirst it acts like a stack.
o
you mean this is a predefined construct that does what I want?
sure but I am trying an exercise that implements this from scratch, that’s why
t
Ah that totally makes sense. Here is what I was thinking if you wanted to stick with your interface. It is kind of cheating if the point is to implement a stack from scratch though, lol! Have fun.
Also, just to answer your original question, I think you figured it out, but you need a getter for your
count
or to turn that into a function. You want the size to be re-evaluated every time you query the count. https://kotlinlang.org/docs/properties.html#getters-and-setters