From 35ae339d65c46e1b51e5659c2040ae5270c93f6f Mon Sep 17 00:00:00 2001 From: Sainan Date: Sun, 17 Nov 2024 08:03:52 +0100 Subject: [PATCH] Document walrus operator for ifstat more clearly --- docs/New Operators.md | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/docs/New Operators.md b/docs/New Operators.md index 5b905e7a..2da803d7 100644 --- a/docs/New Operators.md +++ b/docs/New Operators.md @@ -106,22 +106,13 @@ This operator does not implement any metamethods. ## Walrus Operator The Walrus operator allows you to perform assignments inside of conditional expresssions. -```pluto norun -if a := get_value() then - -- 'a' was assigned a truthy value. -else - -- 'a' was assigned a falsy value. -end -``` -You can imagine it like this, but note they're not always the same: -```pluto norun -do - local a = get_value() - if a then - -- 'a' was assigned a truthy value. - else - -- 'a' was assigned a falsy value. - end +```pluto +local get_value = || -> 1 + +if val := get_value() then -- scope of 'val' begins + print("got value: "..val) +else -- scope of 'val' ends + print("got no value") end ```