NEPLg2 Tutorial - 08 - if の書式バリエーション
Web Playground
Web Playground

if の書式しょしきバリエーション

NEPLg2 の if は、同じ意味を保ったまま複数のレイアウトで書けます。
コードの長さや入れ子の深さに応じて、読みやすい形を選びます。

1行で書く(inline)

TEST
#entry main
#indent 4
#target std

#import "core/math" as *
#import "core/result" as *
#import "std/test" as *

fn clamp_non_negative <(i32)->i32> (x):
    if lt x 0 then 0 else x

fn main <()*>i32> ():
    let checks <Vec<Result<(),str>>>:
        checks_new
        |> checks_push check_eq_i32 0 clamp_non_negative -9
        |> checks_push check_eq_i32 6 clamp_non_negative 6
    let shown <Vec<Result<(),str>>> checks_print_report checks;
    checks_exit_code shown

if: で複数行に分ける

TEST
#entry main
#indent 4
#target std

#import "core/math" as *
#import "core/result" as *
#import "std/test" as *

fn pick <(bool,i32,i32)->i32> (c, a, b):
    if:
        cond c
        then a
        else b

fn main <()*>i32> ():
    let checks <Vec<Result<(),str>>>:
        checks_new
        |> checks_push check_eq_i32 11 pick true 11 22
        |> checks_push check_eq_i32 22 pick false 11 22
    let shown <Vec<Result<(),str>>> checks_print_report checks;
    checks_exit_code shown

then: / else: を block として使う

TEST
#entry main
#indent 4
#target std

#import "core/math" as *
#import "core/result" as *
#import "std/test" as *

fn score <(i32)->i32> (n):
    if:
        cond lt n 0
        then:
            0
        else:
            add n 100

fn main <()*>i32> ():
    let checks <Vec<Result<(),str>>>:
        checks_new
        |> checks_push check_eq_i32 0 score -1
        |> checks_push check_eq_i32 107 score 7
    let shown <Vec<Result<(),str>>> checks_print_report checks;
    checks_exit_code shown

cond / then / else の順序を固定する

if: 形式では、可読性のためにも condthenelse の順序を崩さない運用を推奨します。
then だけ式、else だけ block のような混在も可能です。

TEST
#entry main
#indent 4
#target std

#import "core/math" as *
#import "core/result" as *
#import "std/test" as *

fn adjust <(i32)->i32> (x):
    if:
        cond lt x 0
        then add x 100
        else:
            sub x 100

fn main <()*>i32> ():
    let checks <Vec<Result<(),str>>>:
        checks_new
        |> checks_push check_eq_i32 95 adjust -5
        |> checks_push check_eq_i32 -95 adjust 5
    let shown <Vec<Result<(),str>>> checks_print_report checks;
    checks_exit_code shown