Functions & OOP

Methods

Methods are functions defined inside a class that operate on instance data using the ei (this) keyword.

Defining Methods

Methods are defined using the kaj keyword inside a class:

1sreni Calculator {
2 shuru() {
3 ei.result = 0;
4 }
5
6 kaj add(n) {
7 ei.result = ei.result + n;
8 ferao ei; // Return this for method chaining
9 }
10
11 kaj subtract(n) {
12 ei.result = ei.result - n;
13 ferao ei;
14 }
15
16 kaj multiply(n) {
17 ei.result = ei.result * n;
18 ferao ei;
19 }
20
21 kaj divide(n) {
22 jodi (n != 0) {
23 ei.result = ei.result / n;
24 } nahole {
25 dekho("Cannot divide by zero!");
26 }
27 ferao ei;
28 }
29
30 kaj getResult() {
31 ferao ei.result;
32 }
33
34 kaj reset() {
35 ei.result = 0;
36 ferao ei;
37 }
38}
39
40dhoro calc = notun Calculator();
41dhoro result = calc.add(10).multiply(2).subtract(5).getResult();
42dekho(result); // 15

Accessing Instance Data with 'ei'

The ei keyword (Bengali for "this") refers to the current instance:

1sreni Person {
2 shuru(naam) {
3 ei.naam = naam;
4 ei.friends = [];
5 }
6
7 kaj addFriend(friend) {
8 // 'ei' accesses this instance's properties
9 dhokao(ei.friends, friend);
10 dekho(ei.naam, "added", friend.naam, "as a friend");
11 }
12
13 kaj listFriends() {
14 dekho(ei.naam + "'s friends:");
15 ghuriye (dhoro i = 0; i < dorghyo(ei.friends); i = i + 1) {
16 dekho(" -", ei.friends[i].naam);
17 }
18 }
19}
20
21dhoro rahim = notun Person("Rahim");
22dhoro karim = notun Person("Karim");
23dhoro jamil = notun Person("Jamil");
24
25rahim.addFriend(karim);
26rahim.addFriend(jamil);
27rahim.listFriends();

Method Parameters

1sreni ShoppingCart {
2 shuru() {
3 ei.items = [];
4 ei.total = 0;
5 }
6
7 kaj addItem(name, price, quantity) {
8 dhokao(ei.items, {
9 name: name,
10 price: price,
11 quantity: quantity
12 });
13 ei.total = ei.total + (price * quantity);
14 dekho("Added:", quantity, "x", name, "@ Tk.", price);
15 }
16
17 kaj removeItem(name) {
18 ghuriye (dhoro i = 0; i < dorghyo(ei.items); i = i + 1) {
19 jodi (ei.items[i].name == name) {
20 dhoro item = ei.items[i];
21 ei.total = ei.total - (item.price * item.quantity);
22 // Remove item logic would go here
23 dekho("Removed:", name);
24 ferao sotti;
25 }
26 }
27 ferao mittha;
28 }
29
30 kaj applyDiscount(percentage) {
31 dhoro discount = ei.total * (percentage / 100);
32 ei.total = ei.total - discount;
33 dekho("Applied", percentage, "% discount. Saved Tk.", discount);
34 }
35
36 kaj checkout() {
37 dekho("===== Receipt =====");
38 ghuriye (dhoro i = 0; i < dorghyo(ei.items); i = i + 1) {
39 dhoro item = ei.items[i];
40 dekho(item.quantity, "x", item.name, ":", "Tk.", item.price * item.quantity);
41 }
42 dekho("-------------------");
43 dekho("Total: Tk.", ei.total);
44 }
45}
46
47dhoro cart = notun ShoppingCart();
48cart.addItem("Rice (5kg)", 350, 2);
49cart.addItem("Oil (1L)", 180, 1);
50cart.addItem("Sugar (1kg)", 85, 3);
51cart.applyDiscount(10);
52cart.checkout();

Methods Calling Other Methods

1sreni StringHelper {
2 shuru(text) {
3 ei.text = text;
4 }
5
6 kaj toUpperCase() {
7 ei.text = boroHater(ei.text);
8 ferao ei;
9 }
10
11 kaj toLowerCase() {
12 ei.text = chotoHater(ei.text);
13 ferao ei;
14 }
15
16 kaj trim() {
17 ei.text = chhanto(ei.text);
18 ferao ei;
19 }
20
21 kaj reverse() {
22 dhoro chars = [];
23 ghuriye (dhoro i = dorghyo(ei.text) - 1; i >= 0; i = i - 1) {
24 dhokao(chars, ei.text[i]);
25 }
26 ei.text = joro(chars, "");
27 ferao ei;
28 }
29
30 kaj process() {
31 // Call multiple methods internally
32 ei.trim();
33 ei.toLowerCase();
34 ferao ei;
35 }
36
37 kaj get() {
38 ferao ei.text;
39 }
40}
41
42dhoro helper = notun StringHelper(" HELLO WORLD ");
43dekho(helper.process().get()); // "hello world"
44
45dhoro helper2 = notun StringHelper("Hello");
46dekho(helper2.reverse().toUpperCase().get()); // "OLLEH"

Getter and Setter Pattern

1sreni Temperature {
2 shuru(celsius) {
3 ei.celsius = celsius;
4 }
5
6 // Getter methods
7 kaj getCelsius() {
8 ferao ei.celsius;
9 }
10
11 kaj getFahrenheit() {
12 ferao (ei.celsius * 9/5) + 32;
13 }
14
15 kaj getKelvin() {
16 ferao ei.celsius + 273.15;
17 }
18
19 // Setter methods
20 kaj setCelsius(value) {
21 ei.celsius = value;
22 }
23
24 kaj setFahrenheit(value) {
25 ei.celsius = (value - 32) * 5/9;
26 }
27
28 kaj setKelvin(value) {
29 ei.celsius = value - 273.15;
30 }
31
32 kaj display() {
33 dekho("Temperature:");
34 dekho(" - Celsius:", ei.getCelsius());
35 dekho(" - Fahrenheit:", ei.getFahrenheit());
36 dekho(" - Kelvin:", ei.getKelvin());
37 }
38}
39
40dhoro temp = notun Temperature(25);
41temp.display();
42
43temp.setFahrenheit(98.6); // Body temperature
44temp.display();

Static-Like Behavior

BanglaCode doesn't have static methods, but you can simulate them with module functions:

1// Math utility class
2sreni MathUtils {
3 shuru() {}
4
5 kaj square(n) {
6 ferao n * n;
7 }
8
9 kaj cube(n) {
10 ferao n * n * n;
11 }
12
13 kaj factorial(n) {
14 jodi (n <= 1) {
15 ferao 1;
16 }
17 ferao n * ei.factorial(n - 1);
18 }
19
20 kaj isPrime(n) {
21 jodi (n < 2) {
22 ferao mittha;
23 }
24 ghuriye (dhoro i = 2; i * i <= n; i = i + 1) {
25 jodi (n % i == 0) {
26 ferao mittha;
27 }
28 }
29 ferao sotti;
30 }
31}
32
33dhoro math = notun MathUtils();
34dekho(math.square(5)); // 25
35dekho(math.factorial(5)); // 120
36dekho(math.isPrime(17)); // sotti

Method Overloading Pattern

BanglaCode doesn't support method overloading, but you can use parameter checking:

1sreni Logger {
2 shuru(prefix) {
3 ei.prefix = prefix;
4 }
5
6 kaj log(message, level) {
7 // Default level to "INFO" if not provided
8 jodi (level == khali) {
9 level = "INFO";
10 }
11
12 dekho("[" + level + "]", ei.prefix + ":", message);
13 }
14
15 kaj info(message) {
16 ei.log(message, "INFO");
17 }
18
19 kaj warn(message) {
20 ei.log(message, "WARN");
21 }
22
23 kaj error(message) {
24 ei.log(message, "ERROR");
25 }
26}
27
28dhoro logger = notun Logger("App");
29logger.info("Application started");
30logger.warn("Low memory");
31logger.error("Connection failed");

Builder Pattern

1sreni QueryBuilder {
2 shuru(table) {
3 ei.table = table;
4 ei.columns = "*";
5 ei.whereClause = "";
6 ei.orderClause = "";
7 ei.limitValue = khali;
8 }
9
10 kaj select(columns) {
11 ei.columns = joro(columns, ", ");
12 ferao ei;
13 }
14
15 kaj where(condition) {
16 ei.whereClause = " WHERE " + condition;
17 ferao ei;
18 }
19
20 kaj orderBy(column, direction) {
21 ei.orderClause = " ORDER BY " + column + " " + direction;
22 ferao ei;
23 }
24
25 kaj limit(n) {
26 ei.limitValue = n;
27 ferao ei;
28 }
29
30 kaj build() {
31 dhoro query = "SELECT " + ei.columns + " FROM " + ei.table;
32 query = query + ei.whereClause;
33 query = query + ei.orderClause;
34 jodi (ei.limitValue != khali) {
35 query = query + " LIMIT " + lipi(ei.limitValue);
36 }
37 ferao query;
38 }
39}
40
41dhoro query = notun QueryBuilder("users")
42 .select(["id", "name", "email"])
43 .where("active = 1")
44 .orderBy("name", "ASC")
45 .limit(10)
46 .build();
47
48dekho(query);
49// SELECT id, name, email FROM users WHERE active = 1 ORDER BY name ASC LIMIT 10

Object Utility Methods

BanglaCode provides built-in utility methods for working with objects (maps):

maan - Get Object Values

Returns an array containing all values from an object:

1dhoro person = {
2 "naam": "Rahim",
3 "age": 25,
4 "city": "Dhaka"
5};
6
7dhoro values = maan(person);
8dekho(values); // ["Rahim", 25, "Dhaka"]
9
10// Use values in higher-order functions
11dhoro doubled = manchitro(values, kaj(val) {
12 jodi (dhoron(val) == "NUMBER") {
13 ferao val * 2;
14 }
15 ferao val;
16});
17dekho(doubled); // ["Rahim", 50, "Dhaka"]

jora - Get Object Entries

Returns an array of [key, value] pairs from an object:

1dhoro settings = {
2 "theme": "dark",
3 "notifications": sotti,
4 "fontSize": 14
5};
6
7dhoro entries = jora(settings);
8dekho(entries);
9// [["theme", "dark"], ["notifications", sotti], ["fontSize", 14]]
10
11// Iterate over entries
12proti(entries, kaj(entry) {
13 dekho(entry[0], ":", entry[1]);
14});
15// Output:
16// theme : dark
17// notifications : sotti
18// fontSize : 14
19
20// Filter entries and rebuild object
21dhoro filtered = chhanno(entries, kaj(entry) {
22 ferao entry[0] != "fontSize"; // Exclude fontSize
23});
24dekho(filtered);
25// [["theme", "dark"], ["notifications", sotti]]

mishra - Merge Objects

Merges one or more source objects into a target object. Modifies the target in-place.

1// Basic merge
2dhoro obj1 = {"a": 1, "b": 2};
3dhoro obj2 = {"c": 3};
4dhoro obj3 = {"d": 4};
5
6mishra(obj1, obj2, obj3);
7dekho(obj1); // {"a": 1, "b": 2, "c": 3, "d": 4}
8
9// Later values override earlier ones
10dhoro target = {"name": "Rahim", "age": 25};
11dhoro update = {"age": 26, "city": "Dhaka"};
12
13mishra(target, update);
14dekho(target); // {"name": "Rahim", "age": 26, "city": "Dhaka"}
15
16// Practical: Merge default config with user config
17dhoro defaultConfig = {
18 "theme": "light",
19 "fontSize": 12,
20 "language": "bn"
21};
22
23dhoro userConfig = {
24 "theme": "dark",
25 "fontSize": 14
26};
27
28dhoro finalConfig = {"theme": "light", "fontSize": 12, "language": "bn"};
29mishra(finalConfig, userConfig);
30dekho(finalConfig);
31// {"theme": "dark", "fontSize": 14, "language": "bn"}

Common Object Patterns

Transform Object Values

1dhoro prices = {
2 "apple": 50,
3 "banana": 30,
4 "mango": 80
5};
6
7// Apply discount to all prices
8dhoro values = maan(prices);
9dhoro discounted = manchitro(values, kaj(price) {
10 ferao price * 0.9; // 10% discount
11});
12
13dekho(discounted); // [45, 27, 72]

Filter Object Properties

1dhoro user = {
2 "name": "Rahim",
3 "email": "rahim@example.com",
4 "password": "secret123",
5 "phone": "01712345678",
6 "_internal": "data"
7};
8
9// Remove private fields (starting with _) and sensitive fields
10dhoro filtered = chhanno(jora(user), kaj(entry) {
11 dhoro key = entry[0];
12 ferao key != "password" ebong key[0] != "_";
13});
14
15dekho(filtered);
16// [["name", "Rahim"], ["email", "rahim@example.com"], ["phone", "01712345678"]]

Build Object Dynamically

1// Reduce key-value pairs into object
2dhoro entries = [["id", 1], ["name", "Rahim"], ["active", sotti]];
3
4dhoro obj = sonkuchito(entries, kaj(acc, entry) {
5 acc[entry[0]] = entry[1];
6 ferao acc;
7}, {});
8
9dekho(obj); // {"id": 1, "name": "Rahim", "active": sotti}