tact-lang / tact-docs

Tact main documentation
https://docs.tact-lang.org
40 stars 36 forks source link

What is the scope of functions? #208

Open anton-trunov opened 2 months ago

anton-trunov commented 2 months ago

For instance, if I have something like this:

fun foo() { ... }
fun bar() { ... }

bar should be able to refer to foo, but can foo call bar?

novusnota commented 2 months ago

Can't test in Nujan — doing the following throws RangeError: Maximum call stack size exceeded in Next.js bundled chunks (line:column positions there don't tell anything about the original sources):

import "@stdlib/deploy";

fun foo(a: Int): Int {
    if (a <= 1) { return 1; }
    return bar(a - 1);
}

fun bar(b: Int): Int {
    if (b <= 1 ) { return 1; }
    return foo(b - 1);
}

contract Example with Deployable {
   init() {} // because there's Tact 1.2.0
   get fun call(): Int {
       return foo(10) + bar(10); // should return 2, but doesn't build in Nujan
   }
}

Meanwhile, the following re-write in TypeScript works and produces 2, as it should:

function foo(a: number): number {
    if (a <= 1) { return 1; }
    return bar(a - 1);
}

function bar(b: number): number {
    if (b <= 1 ) { return 1; }
    return foo(b - 1);
}

console.log(foo(10) + bar(10));

I'll investigate this matter locally with Blueprint later on. If that works, I'll file an issue to Nujan