Added namespaces to includes like in Python. (0.14.0)
This commit is contained in:
parent
cb8d5685bb
commit
6bd13f6e96
7 changed files with 406 additions and 17 deletions
57
examples/include_namespace.fun
Executable file
57
examples/include_namespace.fun
Executable file
|
|
@ -0,0 +1,57 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the ISC license.
|
||||
* https://opensource.org/license/isc-license-txt
|
||||
*
|
||||
* Added: 2025-09-30
|
||||
*/
|
||||
|
||||
// Demonstration of the include-as namespace feature.
|
||||
//
|
||||
// Run examples (without installing) by pointing FUN_LIB_DIR to the repo lib:
|
||||
// Linux/macOS/FreeBSD:
|
||||
// FUN_LIB_DIR="$(pwd)/lib" ./build/fun examples/include_namespace.fun
|
||||
// Windows (CMD):
|
||||
// set FUN_LIB_DIR=%CD%\lib && build-debug\fun.exe examples\include_namespace.fun
|
||||
// Windows (PowerShell):
|
||||
// $env:FUN_LIB_DIR="$PWD\lib"; .\build\fun.exe examples\include_namespace.fun
|
||||
|
||||
// Import stdlib math helpers into alias 'm'
|
||||
#include <utils/math.fun> as m
|
||||
|
||||
// Import a local module (this repository file) into alias 'mod'
|
||||
#include "examples/namespaced_mod.fun" as mod
|
||||
|
||||
print("=== include-as namespace demo ===")
|
||||
|
||||
print("Using m.add and m.times from <utils/math.fun>:")
|
||||
print("m.add(2, 3) = " + to_string(m.add(2, 3)))
|
||||
print("m.times(4, 5) = " + to_string(m.times(4, 5)))
|
||||
|
||||
print("")
|
||||
print("Using mod.hello and mod.Greeter from namespaced_mod.fun:")
|
||||
print(mod.hello("Fun"))
|
||||
|
||||
g = mod.Greeter("Hi")
|
||||
g.say("World")
|
||||
|
||||
print("")
|
||||
print("=== done ===")
|
||||
|
||||
/* Expected output:
|
||||
=== include-as namespace demo ===
|
||||
Using m.add and m.times from <utils/math.fun>:
|
||||
m.add(2, 3) = 5
|
||||
m.times(4, 5) = 20
|
||||
|
||||
Using mod.hello and mod.Greeter from namespaced_mod.fun:
|
||||
Hello, Fun!
|
||||
Hi World
|
||||
|
||||
=== done ===
|
||||
*/
|
||||
25
examples/namespaced_mod.fun
Normal file
25
examples/namespaced_mod.fun
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#!/usr/bin/env fun
|
||||
// The shebang line makes no sense here because this is a library which will
|
||||
// never be executed, but it shows that it is not wrong.
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the ISC license.
|
||||
* https://opensource.org/license/isc-license-txt
|
||||
*
|
||||
* Added: 2025-09-30
|
||||
*/
|
||||
|
||||
// namespaced_mod.fun
|
||||
// Simple module to demonstrate include-as namespaces with functions and classes.
|
||||
|
||||
fun hello(name)
|
||||
return "Hello, " + to_string(name) + "!"
|
||||
|
||||
class Greeter(string prefix)
|
||||
// Methods must declare 'this' as the first parameter
|
||||
fun say(this, name)
|
||||
print(this.prefix + " " + to_string(name))
|
||||
0
examples/sha256_str_demo.fun
Normal file → Executable file
0
examples/sha256_str_demo.fun
Normal file → Executable file
Loading…
Add table
Add a link
Reference in a new issue