#How to handle indexing into empty arrays in non verbose way

20 messages · Page 1 of 1 (latest)

reef pendant
#

My issue is that I have a struct that's has bunch of arrays that can be empty. Some of this arrays hold other arrays
I want to access data in this arrays but not error when they are empty. So my issue is that i can't figure out way to do that in non verbose way which would suck to write

fallen osprey
#

arrays can't be empty, you can access them without checking for errors.

reef pendant
#

they can have 0 items that what I'm talking about

kind mauve
#

There's no way to check without an if somewhere, whether you want to make a helper function that checks the length for you is up to you

reef pendant
#

at this point i might just implement Vec and Option goblin

kind mauve
#

I don't know what those are giggle

reef pendant
#

🦀

reef pendant
#

wait wrong code

thin storm
#

I would advise you to try and break the habit of calling slices arrays, they're not the same thing.
I understand that using concepts from other languages seems easier but if they set root they'll be hard to get rid of

(re: deleted code)

reef pendant
#
func ReturnFirstOrDef[T any](arr []T) T {
    var t T
    if len(arr) == 0 {
        return t
    }
    return arr[0]
}
#

here is the code

reef pendant
#

will remember that

#

👍

thin storm
#

not calling things by their proper name is almost always an issue sunturtle
this is one example of why the distinction matters #1138410843302789210 message

#

it's fine to use crutches, you just need to be aware of that so that they don't bite you in the future

reef pendant