More nested function fun! (0.40.1)
This commit is contained in:
parent
be75ee47f8
commit
cf320199ef
6 changed files with 369 additions and 15 deletions
|
|
@ -4,12 +4,18 @@
|
|||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-04-02
|
||||
*/
|
||||
|
||||
/*
|
||||
* Demonstrates nested functions that are only visible inside the
|
||||
* outer function where they are defined. This example avoids
|
||||
* capturing outer variables (closures) and instead passes values
|
||||
* explicitly, which works with the current implementation.
|
||||
*
|
||||
* Added: 2026-04-02
|
||||
*/
|
||||
|
||||
print("=== Nested functions (local to outer function) ===")
|
||||
|
|
@ -19,18 +25,15 @@ fun outer(a, b)
|
|||
// Defined inside outer; not visible as a global symbol
|
||||
fun times3(x)
|
||||
return x * 3
|
||||
end
|
||||
|
||||
// Another local helper; also only visible within outer
|
||||
fun sum_plus1(x, y)
|
||||
return x + y + 1
|
||||
end
|
||||
|
||||
// Use the local helpers
|
||||
t1 = times3(a)
|
||||
t2 = times3(b)
|
||||
return sum_plus1(t1, t2)
|
||||
end
|
||||
|
||||
print("outer(2, 5) -> expected 2*3 + 5*3 + 1 = 22")
|
||||
print(outer(2, 5))
|
||||
|
|
@ -46,19 +49,14 @@ fun demo_deep(n)
|
|||
fun two(y)
|
||||
fun three(z)
|
||||
return z + 1
|
||||
end
|
||||
return three(y) + 1
|
||||
end
|
||||
return two(x) + 1
|
||||
end
|
||||
return one(n)
|
||||
end
|
||||
|
||||
print("demo_deep(4) -> expected 7")
|
||||
print(demo_deep(4))
|
||||
|
||||
/*
|
||||
Expected output:
|
||||
/* Expected output:
|
||||
=== Nested functions (local to outer function) ===
|
||||
outer(2, 5) -> expected 2*3 + 5*3 + 1 = 22
|
||||
22
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue