3 ms·
I see, was under the influence of Java, where almost everything is an object reference. Now how do I return an error in the case that Pop is called on an empty
by MichaelMoser123 5y ago
I see, was under the influence of Java, where almost everything is an object reference.
Now how do I return an error in the case that Pop is called on an empty buffer?
I can return nil for a call on an empty buffer
func (ctx *CBuf[T]) Pop() *T {
but I could return any value for an error, and indicate with the second return value if the call succeeded.
func (ctx *CBuf[T]) Pop() (T, bool) {
I fixed the class, so now it looks as follows: https://github.com/MoserMichael/rzgrep/blob/master/src/cbuf/cbuf.go https://github.com/MoserMichael/rzgrep/blob/master/src/cbuf/...
Thanks!
Still, I am feeling a bit uncomfy with returning a garbage value with the the error indication, even if that counts as being more idiomatic.
I keep wondering, why golang doesn't have an Optional monad in its standard library, like they do everywhere else.
- tgv 5y agoYou can declare a "zero" initialized variable and return that var zero T return zero, false I see that you return .data[0] in your code. It seems to me that that would be out of bounds when someone accidentally creates a buffer with size 0.