Method calls in Soul allow you to invoke functions that are associated with objects, classes, or built-in types. They use dot notation to access and call methods on various data types and objects.
numbers = [1, 2, 3, 4, 5]// Method with single argumentnumbers.push(6) // [1, 2, 3, 4, 5, 6]// Method with multiple argumentsnumbers.splice(2, 1, 10) // Remove 1 element at index 2, insert 10// Method with no argumentslast = numbers.pop() // Remove and return last element
Check object existence: Verify objects exist before calling methods
Handle errors: Use try-catch for risky method calls
Use meaningful names: Choose descriptive method names
Chain appropriately: Use method chaining for fluent interfaces
Validate arguments: Check method arguments before use
Copy
Ask AI
// Good - safe method callingsoul processUserData(user) { if (user == null) { return null } try { // Safe method calls with validation if (typeof user.getName == "function") { name = user.getName() } else { name = "Unknown" } if (typeof user.getEmail == "function") { email = user.getEmail() } else { email = "No email" } return { "name": name, "email": email, "processed": true } } catch (error) { println("Error processing user: " + error) return null }}// Better - with comprehensive validationsoul processUserData(user) { if (user == null || typeof user != "object") { return { error: "Invalid user object" } } result = { "name": "Unknown", "email": "No email", "processed": false } try { if (typeof user.getName == "function") { name = user.getName() if (name != null && name != "") { result.name = name } } if (typeof user.getEmail == "function") { email = user.getEmail() if (email != null && email.contains("@")) { result.email = email } } result.processed = true return result } catch (error) { result.error = "Processing error: " + error return result }}
Method calls are fundamental to object-oriented programming in Soul, providing a clean and intuitive way to interact with objects and their functionality.