Objects in SwaziLang
What Is an Object?
Think about a real-world "thing" - like a person. A person has:
- Properties: name, age, height, email
- Actions/Methods: can walk, can talk, can learn
An object in programming is similar. It's a collection of related data (properties) and actions (methods) bundled together.
Instead of having separate variables like:
data student_name = "Hassan";
data student_age = 20;
data student_grade = "A";You can group them together in an object:
data student = {
jina: "Hassan",
umri: 20,
grade: "A"
};Much cleaner, right?
Creating Objects
Basic Object Syntax
data student = {
jina: "Hassan",
umri: 20,
grade: "A"
};Structure:
- Use curly braces
{ } - Inside, list key-value pairs
- Separate pairs with commas
- Format:
key: value
More Examples
data book = {
title: "Learning SwaziLang",
author: "Expert",
pages: 350,
available: kweli
};
data location = {
city: "Dar es Salaam",
country: "Tanzania",
latitude: -6.8,
longitude: 39.2
};Empty Object
data empty = {};Accessing Object Properties
Dot Notation (Most Common)
data student = {
jina: "Hassan",
umri: 20
};
chapisha student.jina; // Prints: Hassan
chapisha student.umri; // Prints: 20Bracket Notation
data student = {
jina: "Hassan",
umri: 20
};
chapisha student["jina"]; // Prints: Hassan
chapisha student["umri"]; // Prints: 20When to use bracket notation:
- When the property name has spaces or special characters
- When you're storing the property name in a variable
data person = {
"full name": "Hassan Ali",
"email address": "hassan@example.com"
};
chapisha person["full name"]; // Hassan Ali
// Or with a variable:
data key = "email address";
chapisha person[key]; // hassan@example.comModifying Object Properties
You can change property values:
data student = {
jina: "Hassan",
umri: 20
};
student.umri = 21;
chapisha student.umri; // 21
student["jina"] = "Ali";
chapisha student.jina; // AliAdding New Properties
You can add properties that didn't exist before:
data student = {
jina: "Hassan"
};
student.umri = 20; // Add new property
student.email = "hassan@example.com";
chapisha student;
// { jina: "Hassan", umri: 20, email: "hassan@example.com" }Using Operations on Properties
data account = {
balance: 1000
};
account.balance += 500;
chapisha account.balance; // 1500
account.balance *= 2;
chapisha account.balance; // 3000Object Methods (Tabia)
Objects can contain methods - functions that do actions on the object's data.
Basic Method Definition
data student = {
jina: "Hassan",
umri: 20,
tabia greet {
chapisha "Habari, nina jina " + $.jina;
}
};
// Call the method
student.greet(); // Prints: Habari, nina jina HassanMethods with Parameters
data calculator = {
value: 0,
tabia add amount {
$.value += amount;
rudisha $; // Return the object for chaining
},
tabia display {
chapisha $.value;
}
};
calculator.add(10);
calculator.display(); // 10
calculator.add(5);
calculator.display(); // 15Understanding $ - Reference to the Object
Inside a method, $ refers to the object itself:
data person = {
jina: "Hassan",
umri: 25,
tabia introduce {
chapisha "Mimi ni " + $.jina + " mwenye miaka " + $.umri;
}
};
person.introduce(); // Prints: Mimi ni Hassan mwenye miaka 25Method Chaining with rudisha $
By returning $ from a method, you can chain method calls:
data calculator = {
result: 0,
tabia add x {
$.result += x;
rudisha $; // Return the object
},
tabia multiply x {
$.result *= x;
rudisha $;
},
tabia display {
chapisha $.result;
}
};
// Chain methods!
calculator.add(5).multiply(2).add(3).display();
// Step 1: 0 + 5 = 5
// Step 2: 5 * 2 = 10
// Step 3: 10 + 3 = 13
// Prints: 13Nested Objects
Objects can contain other objects:
data student = {
jina: "Hassan",
contact: {
email: "hassan@example.com",
phone: "0654321789"
},
address: {
city: "Dar es Salaam",
country: "Tanzania"
}
};
chapisha student.jina; // Hassan
chapisha student.contact.email; // hassan@example.com
chapisha student.address.city; // Dar es SalaamAccessing with Variables
data person = {
jina: "Ali",
location: {
city: "Zanzibar"
}
};
data prop = "location";
data nested = "city";
chapisha person[prop][nested]; // ZanzibarPractical Examples
Example 1: Student Object
data student = {
jina: "Hassan",
umri: 20,
grade: "A",
tabia show_info {
chapisha "Jina: " + $.jina;
chapisha "Umri: " + $.umri;
chapisha "Grade: " + $.grade;
},
tabia pass_exam new_grade {
$.grade = new_grade;
chapisha $.jina + " sasa ana grade " + $.grade;
}
};
student.show_info();
// Prints:
// Jina: Hassan
// Umri: 20
// Grade: A
student.pass_exam("B");
// Prints: Hassan sasa ana grade BExample 2: Bank Account
data account = {
account_number: "12345",
balance: 5000,
owner: "Ali",
tabia deposit amount {
$.balance += amount;
chapisha "Deposit: " + amount + ". New balance: " + $.balance;
rudisha $;
},
tabia withdraw amount {
kama amount <= $.balance {
$.balance -= amount;
chapisha "Withdrawal: " + amount + ". New balance: " + $.balance;
} vinginevyo {
chapisha "Insufficient funds!";
}
rudisha $;
},
tabia check_balance {
chapisha "Account: " + $.owner;
chapisha "Balance: " + $.balance;
}
};
account.deposit(1000).withdraw(500).check_balance();Example 3: Product in a Store
data product = {
jina: "Laptop",
bei: 800000,
stock: 5,
tabia reduce_stock quantity {
kama quantity <= $.stock {
$.stock -= quantity;
rudisha kweli;
} vinginevyo {
rudisha sikweli;
}
},
tabia get_info {
chapisha "Product: " + $.jina;
chapisha "Price: " + $.bei;
chapisha "Stock: " + $.stock;
}
};
product.get_info();
data sold = product.reduce_stock(2);
kama sold {
chapisha "Sold 2 units!";
} vinginevyo {
chapisha "Not enough stock!";
}
product.get_info();Example 4: Rectangle with Calculations
data rect = {
length: 10,
width: 5,
tabia area {
rudisha $.length * $.width;
},
tabia perimeter {
rudisha 2 * ($.length + $.width);
},
tabia display {
chapisha "Length: " + $.length;
chapisha "Width: " + $.width;
chapisha "Area: " + $.area();
chapisha "Perimeter: " + $.perimeter();
}
};
rect.display();
// Prints:
// Length: 10
// Width: 5
// Area: 50
// Perimeter: 30Important Notes
1. Properties Can Be Any Type
data mixed = {
name: "Hassan",
age: 25,
scores: [85, 90, 78],
active: kweli,
nothing: null,
details: {
city: "Dar",
country: "Tanzania"
}
};2. Accessing Non-Existent Properties
data person = {
jina: "Hassan"
};
chapisha person.email; // Prints: null (doesn't exist)3. Methods Can Call Other Methods
data calculator = {
value: 0,
tabia add x {
$.value += x;
rudisha $;
},
tabia display {
chapisha $.value;
},
tabia add_and_show x {
$.add(x);
$.display();
}
};
calculator.add_and_show(5); // Adds 5 and displays it4. Objects Are Reference Types
When you assign an object to another variable, both refer to the same object:
data obj1 = { value: 10 };
data obj2 = obj1;
obj2.value = 20;
chapisha obj1.value; // 20 (both point to same object!)5. Method Declaration Syntax
Methods in objects are declared using the tabia keyword:
data obj = {
tabia method_name param1 param2 {
// Method body
chapisha "Hello";
}
};For methods without parameters:
data obj = {
tabia method_name {
chapisha "Hello";
}
};Practice Challenges
Challenge 1: Create a Simple Object
Create a book object with title, author, and pages. Add a method to display the information:
Solution:
data book = {
title: "Learning SwaziLang",
author: "Expert",
pages: 350,
tabia display {
chapisha "Title: " + $.title;
chapisha "Author: " + $.author;
chapisha "Pages: " + $.pages;
}
};
book.display();Challenge 2: Object with Methods That Modify
Create a counter object that can increment, decrement, and display:
Solution:
data counter = {
count: 0,
tabia increment {
$.count += 1;
rudisha $;
},
tabia decrement {
$.count -= 1;
rudisha $;
},
tabia display {
chapisha "Count: " + $.count;
}
};
counter.increment().increment().decrement().display();
// Prints: Count: 1Challenge 3: Nested Objects
Create a company object with a ceo object inside it:
Solution:
data company = {
name: "TechCorp",
ceo: {
jina: "Hassan",
email: "hassan@techcorp.com"
},
tabia show_ceo {
chapisha "CEO: " + $.ceo.jina;
chapisha "Email: " + $.ceo.email;
}
};
company.show_ceo();Challenge 4: Method Chaining
Create a string_builder object with methods that return $ for chaining:
Solution:
data builder = {
text: "",
tabia add str {
$.text += str;
rudisha $;
},
tabia add_space {
$.text += " ";
rudisha $;
},
tabia result {
chapisha $.text;
}
};
builder.add("Hello").add_space().add("World").result();
// Prints: Hello WorldWhat's Next?
In the next chapter, you'll learn about:
- Object Privacy - Using
@for private properties and methods (private methods or roperties not accassible outiside) - Locked Properties - Using
&for locked members (can not be overwritten/reassigned but visible outiside) - Advanced Object Patterns - More complex object structures
Objects are fundamental to organizing code. They let you group related data and behavior together, making programs easier to understand and maintain!
Remember: Objects let you bundle related data and methods together. Use tabia to declare methods, use $ inside methods to refer to the object itself, and return $ for method chaining!