Skip to content

Commit 3e93ce5

Browse files
committed
Polish class object docs
1 parent a925831 commit 3e93ce5

File tree

2 files changed

+67
-72
lines changed

2 files changed

+67
-72
lines changed

docs/guide/language/basic.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ let a = 1 in [1, 2, 3];
154154
let b = 1 not in [1, 2, 3];
155155
```
156156

157-
:::danger
157+
:::danger Not supported
158158
AIScript doesn't support increment/decrement operators like `i++`, `++i`, `i--` or `--i`.
159159
:::
160160

docs/guide/language/class-object.md

Lines changed: 66 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,29 @@ AIScript offers a powerful class-based object-oriented programming model inspire
44

55
## Defining a Class
66

7-
Use the `class` keyword to define a class with properties and methods:
7+
Use the `class` keyword to define a class with properties and methods.
88

9-
```js
9+
```rust
1010
class Person {
1111
name: str,
1212
age: int,
13-
13+
1414
fn new(name: str, age: int) {
1515
self.name = name;
1616
self.age = age;
1717
}
18-
19-
fn greet() -> str {
20-
return "Hello, my name is {self.name} and I'm {self.age} years old.";
21-
}
2218
}
2319
```
2420

25-
The `new` method is a special constructor that initializes a new instance of the class.
21+
The `new()` method is a special constructor that initializes a new instance of the class.
2622

2723
## Creating Objects
2824

2925
Create a new instance of a class using the class name followed by parentheses containing constructor arguments:
3026

3127
```js
3228
let alice = Person("Alice", 30);
33-
print(alice.greet()); // "Hello, my name is Alice and I'm 30 years old."
29+
print(alice); // Person {name: Alice, age: 30}
3430
```
3531

3632
You can also use object literal syntax with type checking:
@@ -41,8 +37,37 @@ let bob = Person {
4137
age: 25,
4238
};
4339

44-
print(bob.greet()); // "Hello, my name is Bob and I'm 25 years old."
40+
print(bob); // Person {name: Bob, age: 25}
41+
```
42+
43+
## Methods
44+
45+
Define methods within a class using the `fn` keyword, declare `self` as the first argument to make the method a instance method, otherwise, it's a static method:
46+
47+
```js
48+
class Person {
49+
name: str,
50+
age: int,
51+
52+
fn new(name: str, age: int) {
53+
self.name = name;
54+
self.age = age;
55+
}
56+
57+
fn greet(self) -> str {
58+
print("Hello, my name is", self.name, "and I'm", self.age, "years old.");
59+
}
60+
61+
fn static_method() {
62+
print("static method of Person");
63+
}
64+
}
65+
66+
let alice = Person("Alice", 30);
67+
alice.greet(); // "Hello, my name is Alice and I'm 30 years old."
68+
Person.static_method(); // "static method of Person"
4569
```
70+
Use `class_instance.method()` to call an instance method, use `ClassName.method()` to call a static method.
4671

4772
## Accessing Properties and Methods
4873

@@ -63,46 +88,62 @@ AIScript supports field validation using directive annotations similar to Python
6388
class User {
6489
@string(min_len=3, max_len=50)
6590
username: str,
66-
67-
@string(pattern="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$")
68-
email: str,
69-
91+
7092
@number(min=0, max=120)
7193
age: int,
7294
}
7395

7496
// This will throw a validation error
7597
let invalid_user = User {
7698
username: "a",
77-
email: "not-an-email",
7899
age: 150,
79100
} |err| {
80-
print("Validation error:", err);
101+
print(err);
81102
};
82103
```
83104

105+
Above code will throw a validation error:
106+
107+
```json
108+
ValidationError! {
109+
errors: [
110+
{
111+
msg: Number is greater than the maximum value of 120,
112+
loc: [age],
113+
type: validation_error,
114+
input: 150
115+
},
116+
{
117+
msg: String length is less than the minimum length of 3,
118+
loc: [username],
119+
type: validation_error,
120+
input: a
121+
}
122+
]
123+
}
124+
```
125+
84126
## Inheritance
85127

86128
Use the parentheses syntax to inherit from another class:
87129

88130
```js
89131
class Employee(Person) {
90-
@string
91132
job_title: str,
92-
133+
93134
fn new(name: str, age: int, job_title: str) {
94135
// Call the parent class constructor
95136
super.new(name, age);
96137
self.job_title = job_title;
97138
}
98-
99-
fn greet() -> str {
100-
return "Hello, I'm {self.name}, a {self.job_title}.";
139+
140+
fn greet(self) -> str {
141+
print("Hello, my name is", self.name,"and I'm", self.age, "years old, a", self.job_title);
101142
}
102143
}
103144

104145
let developer = Employee("Carlos", 28, "Software Developer");
105-
print(developer.greet()); // "Hello, I'm Carlos, a Software Developer."
146+
developer.greet(); // "Hello, my name is Carlos and I'm 28 years old, a Software Developer"
106147
```
107148

108149
:::danger
@@ -118,64 +159,18 @@ class Config {
118159
host: str = "localhost",
119160
port: int = 8080,
120161
debug: bool = false,
121-
122-
fn get_url() -> str {
123-
return "http://{self.host}:{self.port}";
124-
}
125162
}
126163

127164
// Use defaults
128165
let default_config = Config();
129-
print(default_config.get_url()); // "http://localhost:8080"
166+
print(default_config); // Config {host: localhost, debug: false, port: 8080}
130167

131168
// Override defaults
132169
let custom_config = Config {
133170
host: "example.com",
134171
port: 443,
135172
};
136-
print(custom_config.get_url()); // "http://example.com:443"
137-
```
138-
139-
## Error Handling in Methods
140-
141-
Methods can declare and handle errors using the same error handling syntax as regular functions:
142-
143-
```js
144-
enum NetworkError! {
145-
ConnectionFailed,
146-
Timeout,
147-
ServerError(str),
148-
}
149-
150-
class APIClient {
151-
base_url: str,
152-
153-
fn new(base_url: str) {
154-
self.base_url = base_url;
155-
}
156-
157-
fn fetch(endpoint: str) -> str | NetworkError! {
158-
if self.base_url == "" {
159-
raise NetworkError!::ServerError("Base URL is not set");
160-
}
161-
162-
// Simulate network request
163-
if endpoint == "/error" {
164-
raise NetworkError!::ConnectionFailed;
165-
}
166-
167-
return "Response from {self.base_url}{endpoint}";
168-
}
169-
}
170-
171-
let client = APIClient("https://api.example.com");
172-
173-
let result = client.fetch("/users") |err| {
174-
print("Error fetching data:", err);
175-
return "Failed to fetch";
176-
};
177-
178-
print(result); // "Response from https://api.example.com/users"
173+
print(custom_config); // Config {host: example.com, debug: false, port: 443}
179174
```
180175

181-
This overview covers the fundamentals of class and object-oriented programming in AIScript. By combining these features with AIScript's error handling, validation, and type system, you can create clean, maintainable, and robust code.
176+
This overview covers the fundamentals of class and object-oriented programming in AIScript. By combining these features with AIScript's error handling, validation, and type system, you can create clean, maintainable, and robust code.

0 commit comments

Comments
 (0)