#What are my options (pun intended) - unwrapping multiple optionals

1 messages · Page 1 of 1 (latest)

vital pike
#

(I like my second answer better) || oh god, you want it to work unless all 3 are null or empty? In that case, I think what you're doing is probably fine, but I hope that your problem isn't so interdependent as that.

Also, though I don't think it's so useful here, orelse is the easy way to work with optionals. You could do something like this, I guess, though I think it's less readable:

const interfaces_empty = if (interfaces) |i| i.len == 0 else true;
// equivalent to
const interfaces_empty = (interfaces orelse &.{}).len == 0;
``` ||
#

ah, actually, you could just do this:

const interfaces = try parseImplementsInterfaces(p) orelse &.{};
const directives = try parseConstDirectives(p) orelse &.{};
const fields = try parseFieldsDefinition(p) orelse &.{};

if (interfaces.len == 0 and directives.len == 0 and fields.len == 0) {
    return error.UnexpectedToken;
}

of course, that assumes that a null slice and an empty slice are equivalent for your problem, but it looks like they probably are based on your logic.