Skip to content

Statement Continuation Operator

IsaacShelton edited this page Mar 21, 2022 · 1 revision

Statement Continuation Operator

The statement continuation operator ; can be used to have multiple statements where only one would normally be allowed.

myFirstStatement(); mySecondStatement()

Purpose

The most common usage case for ; is having two statements for a single-line conditional.

if should_exit, cleanup(); return FAILURE

which is equivalent to

if should_exit {
    cleanup()
    return FAILURE
}

Another usage case is to have multiple small statements on the same line.

struct Vector3f (x, y, z float)

func vector3f(x, y, z float) Vector3f {
    v Vector3f
    v.x = x; v.y = y; v.z = z
    return v
}

Extraneous Semicolons

The ; operator is ignored if the previous statement is a part of a block, for example

doSomething1();
doSomething2();
doSomething3();

is the same as

doSomething1()
doSomething2()
doSomething3()

However, when in single line conditionals, this is not the case

func doThings {
    if should_do_somthing, doSomething1();
    
    doSomething2();
    doSomething3();
}

is the same as saying

func doThings {
    if should_do_somthing {
        doSomething1()    
        doSomething2()
        doSomething3()
    }
}

In order to avoid confusion, it's best to avoid using semicolons when they have no effect.

Clone this wiki locally