Advanced

Modules

BanglaCode supports a module system for organizing code into separate files. Use pathao (export) and ano (import) to share code between files.

Exporting (pathao)

Use the pathao keyword (meaning "send") to export functions, variables, or classes:

math.bangbanglacode
1// Export a function
2pathao kaj add(a, b) {
3 ferao a + b;
4}
5
6pathao kaj subtract(a, b) {
7 ferao a - b;
8}
9
10pathao kaj multiply(a, b) {
11 ferao a * b;
12}
13
14pathao kaj divide(a, b) {
15 jodi (b == 0) {
16 felo "Cannot divide by zero";
17 }
18 ferao a / b;
19}
20
21// Export a constant
22pathao dhoro PI = 3.14159;
23pathao dhoro E = 2.71828;

Exporting Classes

shapes.bangbanglacode
1pathao sreni Rectangle {
2 shuru(width, height) {
3 ei.width = width;
4 ei.height = height;
5 }
6
7 kaj area() {
8 ferao ei.width * ei.height;
9 }
10
11 kaj perimeter() {
12 ferao 2 * (ei.width + ei.height);
13 }
14}
15
16pathao sreni Circle {
17 shuru(radius) {
18 ei.radius = radius;
19 }
20
21 kaj area() {
22 ferao 3.14159 * ei.radius * ei.radius;
23 }
24
25 kaj circumference() {
26 ferao 2 * 3.14159 * ei.radius;
27 }
28}

Importing (ano)

Use the ano keyword (meaning "bring") with hisabe(meaning "as") to import modules with an alias:

main.bangbanglacode
1// Import with alias
2ano "math.bang" hisabe math;
3
4// Use imported functions
5dekho(math.add(5, 3)); // 8
6dekho(math.multiply(4, 7)); // 28
7dekho(math.PI); // 3.14159
8
9// Import shapes module
10ano "shapes.bang" hisabe shapes;
11
12dhoro rect = notun shapes.Rectangle(10, 5);
13dekho("Rectangle area:", rect.area()); // 50
14
15dhoro circle = notun shapes.Circle(7);
16dekho("Circle area:", circle.area()); // ~153.94

Import Paths

1// Relative imports
2ano "utils.bang" hisabe utils; // Same directory
3ano "./helpers/string.bang" hisabe str; // Subdirectory
4ano "../common/config.bang" hisabe cfg; // Parent directory
5
6// Use imported modules
7dhoro result = utils.processData(data);
8dhoro formatted = str.capitalize(text);
9dekho(cfg.APP_NAME);

Module Organization

A well-organized project structure:

project/
├── main.bang
├── config.bang
├── utils/
│   ├── string.bang
│   ├── array.bang
│   └── math.bang
├── models/
│   ├── user.bang
│   └── product.bang
└── services/
    ├── auth.bang
    └── database.bang

Example: Utils Module

utils/string.bangbanglacode
1pathao kaj capitalize(str) {
2 jodi (dorghyo(str) == 0) {
3 ferao str;
4 }
5 ferao boroHater(str[0]) + chotoHater(angsho(str, 1));
6}
7
8pathao kaj repeat(str, times) {
9 dhoro result = "";
10 ghuriye (dhoro i = 0; i < times; i = i + 1) {
11 result = result + str;
12 }
13 ferao result;
14}
15
16pathao kaj truncate(str, maxLen) {
17 jodi (dorghyo(str) <= maxLen) {
18 ferao str;
19 }
20 ferao angsho(str, 0, maxLen - 3) + "...";
21}
main.bangbanglacode
1ano "utils/string.bang" hisabe strUtils;
2
3dekho(strUtils.capitalize("hello")); // "Hello"
4dekho(strUtils.repeat("ab", 3)); // "ababab"
5dekho(strUtils.truncate("Hello World", 8)); // "Hello..."

JSON Imports

You can import JSON files as data:

config.jsonjson
1{
2 "app_name": "MyApp",
3 "version": "1.0.0",
4 "database": {
5 "host": "localhost",
6 "port": 5432
7 },
8 "features": {
9 "dark_mode": true,
10 "notifications": true
11 }
12}
main.bangbanglacode
1// Import JSON as object
2ano "config.json" hisabe config;
3
4dekho("App:", config.app_name);
5dekho("Version:", config.version);
6dekho("DB Host:", config.database.host);
7
8jodi (config.features.dark_mode) {
9 dekho("Dark mode is enabled");
10}

Module Patterns

Service Module

services/auth.bangbanglacode
1dhoro users = {};
2dhoro sessions = {};
3
4pathao kaj register(username, password) {
5 jodi (users[username] != khali) {
6 felo "User already exists";
7 }
8 users[username] = {
9 password: password,
10 createdAt: somoy()
11 };
12 dekho("User registered:", username);
13 ferao sotti;
14}
15
16pathao kaj login(username, password) {
17 dhoro user = users[username];
18 jodi (user == khali) {
19 felo "User not found";
20 }
21 jodi (user.password != password) {
22 felo "Invalid password";
23 }
24
25 dhoro sessionId = lipi(somoy()) + "_" + username;
26 sessions[sessionId] = {
27 username: username,
28 loginTime: somoy()
29 };
30 ferao sessionId;
31}
32
33pathao kaj isLoggedIn(sessionId) {
34 ferao sessions[sessionId] != khali;
35}
36
37pathao kaj logout(sessionId) {
38 sessions[sessionId] = khali;
39 dekho("Logged out");
40}
main.bangbanglacode
1ano "services/auth.bang" hisabe auth;
2
3// Register and login
4auth.register("rahim", "password123");
5dhoro session = auth.login("rahim", "password123");
6
7dekho("Logged in:", auth.isLoggedIn(session)); // sotti
8
9auth.logout(session);
10dekho("Logged in:", auth.isLoggedIn(session)); // mittha

Model Module

models/user.bangbanglacode
1pathao sreni User {
2 shuru(id, naam, email) {
3 ei.id = id;
4 ei.naam = naam;
5 ei.email = email;
6 ei.createdAt = somoy();
7 }
8
9 kaj toJSON() {
10 ferao {
11 id: ei.id,
12 naam: ei.naam,
13 email: ei.email,
14 createdAt: ei.createdAt
15 };
16 }
17
18 kaj validate() {
19 jodi (dorghyo(ei.naam) < 2) {
20 ferao {valid: mittha, error: "Name too short"};
21 }
22 jodi (khojo(ei.email, "@") < 0) {
23 ferao {valid: mittha, error: "Invalid email"};
24 }
25 ferao {valid: sotti};
26 }
27}

Circular Import Protection

BanglaCode has built-in protection against circular imports. If module A imports B and B imports A, the interpreter handles it gracefully:

1// The interpreter caches loaded modules to prevent
2// infinite loops from circular imports.
3// Each module is only executed once, and subsequent
4// imports return the cached exports.

Best Practices

  • One responsibility per module - Keep modules focused on a single task
  • Use meaningful names - Module names should describe their purpose
  • Export only what's needed - Keep internal helpers private
  • Document exports - Add comments explaining what each export does
  • Avoid circular dependencies - Structure code to prevent circular imports