#!/usr/bin/ruby

# Static and constant initialization

var counter = 0;

func test(type){
    say "#{type} call";
    if (counter++ > 3) {
        die "ERROR!";
    }
    return "...";
}

func init() {
    static a = test('static');  # called only once
    const  b = test('const');   # called dynamically each time

    a := test("static :=");     # should not change if defined

    say "#{a} #{b}";
}

init();
init();
init();

#
## Lexical const test
#
func lex_const() {
    func(a) {
        const x = a
        x
    }
}

assert_eq(lex_const()(3), 3)
assert_eq(lex_const()(4), 4)

#
## Static inside a closure
#

func lex_static() {
    func {
        static a = 42
        ++a
    }
}

assert_eq(lex_static()(), 43)
assert_eq(lex_static()(), 43)

var l = lex_static()
assert_eq(l(), 43)
assert_eq(l(), 44)
assert_eq(l(), 45)

say "** Test passed!";