#unwrap multiple optionals in one if

1 messages · Page 1 of 1 (latest)

random imp
#

trying to do something like this
if (v1.x and v1.y and v2.x and v2.y) |x1, y2, x2, y2| {draw.line(x1, y1, x2, y2);}
but neither the and nor , syntax work. anyone know if this is possible?

gusty crane
#

You could do

blk: {
    const x1 = v1.x orelse break :blk;
    const y1 = v1.y orelse break :blk;
    const x2 = v2.x orelse break :blk;
    const y2 = v2.y orelse break :blk;
    draw.line(x1, y1, x2, y2);
}```
hexed barn
#

itd have to be blk: { and break :blk;

random imp
#

i wanna only not draw the lines where a point is missing

gusty crane
#

Ah forgot that sorry

random imp
#

theres 3 of them

#

so i cant break

hexed barn
#

this is breaking out of a block, not a loop

random imp
#

oh didnt notice

#

yeah that could work

#

wish there was a cleaner way tho

gusty crane
#

You could also do

if (v1.x != null and v1.y != null and v2.x != null and v2.y != null) draw.line(v1.x.?, v1.y.?, v2.x.?, v2.y.?);```
random imp
#

yep thats better