#expression if

1 messages · Page 1 of 1 (latest)

brazen wadi
#

is there anything like the ternary operator in C?

tiny saffron
#

x = if (condition) 1 else 2

brazen wadi
#

neat thanks

tiny saffron
#

Switches and blocks can also return expressions

const x = switch (thing) {
    a => 1,
    b => 1000,
};

const y = blk: {
    doThing(&z);
    break :blk z;
};
normal sable
#

also loops:

const index: ?T = for (items) |elem| {
    if (std.meta.eql(elem, key)) break elem;
} else null;