Skip to content

Commit 0411684

Browse files
committed
docs: expand built-in types documentation with detailed reference for String, Nullable, and System Library
Enhance the documentation for built-in types in Ovum by providing comprehensive details on the String type, including its methods and usage examples. Expand the Nullable types section to clarify their creation, method call restrictions, and null handling operators. Additionally, significantly enrich the System Library documentation with extensive information on I/O operations, time functions, file handling, process control, and system information, ensuring clarity and usability for developers.
1 parent c0f780f commit 0411684

File tree

3 files changed

+535
-22
lines changed

3 files changed

+535
-22
lines changed

docs/reference/builtin_types.md

Lines changed: 223 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,225 @@
11
# Built-in Reference Types
22

3-
* `String` *(reference type; not primitive)* - text data
4-
* *Nullable* `Int?` (and all other primitive types) - `Int` or `null` - also passed by reference
5-
* **Array classes (no templates / generics):**
6-
* For primitives: `IntArray`, `FloatArray`, `BoolArray`, `CharArray`, `ByteArray`, `PointerArray`
7-
* For objects: **`ObjectArray`**
8-
* **Convenience**: `StringArray` (array of `String`, used for `Main`)
3+
This document describes the built-in reference types in Ovum, their methods, and the standard interfaces that all types implement.
4+
5+
## String Type
6+
7+
`String` is a reference type (not primitive) for text data. All strings are immutable by default and implement `IStringConvertible`, `IComparable`, and `IHashable`.
8+
9+
### String Methods
10+
11+
* `Length(): Int` - Returns the number of characters in the string
12+
* `ToString(): String` - Returns the string itself (implements `IStringConvertible`)
13+
* `IsLess(other: Object): Bool` - Lexicographic comparison (implements `IComparable`)
14+
* `GetHash(): Int` - Returns hash code for the string (implements `IHashable`)
15+
* `+` operator - String concatenation (e.g., `"Hello" + " " + "World"`)
16+
17+
### String Usage
18+
19+
```ovum
20+
val greeting: String = "Hello, World!"
21+
val length: Int = greeting.Length() // Returns 13
22+
val combined: String = "Hello" + ", " + "World!"
23+
24+
// String comparison
25+
val a: String = "apple"
26+
val b: String = "banana"
27+
val isLess: Bool = a.IsLess(b) // true (lexicographic order)
28+
29+
// String hashing
30+
val hash: Int = greeting.GetHash()
31+
```
32+
33+
## Nullable Types
34+
35+
> **Note**: For detailed information about nullable types, see [`nullable.md`](nullable.md). This section only covers basic information.
36+
37+
Any primitive type can be made nullable by appending `?` (e.g., `Int?`, `String?`). Nullable types are passed by reference and can hold either a value or `null`.
38+
39+
**Important**: You cannot directly call methods on nullable types using `.` - you must use the safe call operator `?.` or non-null assertion `!!`.
40+
41+
```ovum
42+
val nullableString: String? = "Hello"
43+
// val length: Int = nullableString.Length() // ERROR: Cannot call method directly on nullable
44+
val safeLength: Int = nullableString?.Length() ?: 0 // Correct: Use safe call
45+
val forcedLength: Int = nullableString!!.Length() // Correct: Use non-null assertion
46+
```
47+
48+
## Array Types
49+
50+
Ovum provides specialized array classes for different data types. Arrays are reference types and support indexing, iteration, and length operations. All array types implement `IStringConvertible`, `IComparable`, and `IHashable`.
51+
52+
### Primitive Arrays
53+
54+
* `IntArray` - Array of 64-bit signed integers
55+
* `FloatArray` - Array of 64-bit floating-point numbers
56+
* `BoolArray` - Array of Boolean values
57+
* `CharArray` - Array of characters
58+
* `ByteArray` - Array of 8-bit unsigned integers
59+
* `PointerArray` - Array of raw memory addresses (unsafe)
60+
61+
### Object Arrays
62+
63+
* `ObjectArray` - Array of any object type (implements `Object`)
64+
* `StringArray` - Convenience array of `String` objects (used for `Main` function)
65+
66+
## File Type
67+
68+
`File` is a reference type for file operations. Files are nullable by default (`File?`) since file operations can fail. The `File` type implements `IStringConvertible`, `IComparable`, and `IHashable`.
69+
70+
### File Methods
71+
72+
* `ReadAllBytes(): ByteArray?` - Reads all bytes from the file, returns `null` on error
73+
* `ReadAllText(): String?` - Reads all text from the file as UTF-8, returns `null` on error
74+
* `WriteAllBytes(data: ByteArray): Bool` - Writes all bytes to the file, returns `false` on error
75+
* `WriteAllText(text: String): Bool` - Writes all text to the file as UTF-8, returns `false` on error
76+
* `AppendText(text: String): Bool` - Appends text to the file, returns `false` on error
77+
* `Close(): Void` - Closes the file handle
78+
* `IsOpen(): Bool` - Returns `true` if the file is currently open
79+
* `GetSize(): Int?` - Returns file size in bytes, or `null` if error
80+
* `ToString(): String` - Returns file path (implements `IStringConvertible`)
81+
* `IsLess(other: Object): Bool` - Compares file paths lexicographically (implements `IComparable`)
82+
* `GetHash(): Int` - Returns hash of file path (implements `IHashable`)
83+
84+
### File Usage
85+
86+
```ovum
87+
// File operations
88+
val file: File? = sys::OpenFile("data.txt", sys::FileMode::Read)
89+
if (file != null) {
90+
val content: String? = file.ReadAllText()
91+
if (content != null) {
92+
sys::Print("File content: " + content)
93+
}
94+
file.Close()
95+
}
96+
97+
// Writing to file
98+
val outputFile: File? = sys::OpenFile("output.txt", sys::FileMode::Write)
99+
if (outputFile != null) {
100+
val success: Bool = outputFile.WriteAllText("Hello, World!")
101+
if (success) {
102+
sys::Print("File written successfully")
103+
}
104+
outputFile.Close()
105+
}
106+
107+
// File comparison
108+
val file1: File? = sys::OpenFile("a.txt", sys::FileMode::Read)
109+
val file2: File? = sys::OpenFile("b.txt", sys::FileMode::Read)
110+
if (file1 != null && file2 != null) {
111+
val isLess: Bool = file1.IsLess(file2) // Compares file paths
112+
}
113+
```
114+
115+
### Array Methods
116+
117+
All array types support the following methods:
118+
119+
* `Length(): Int` - Returns the number of elements in the array
120+
* `[index]: ElementType` - Indexing operator for element access
121+
* `[index] = value` - Assignment operator for mutable arrays
122+
* `ToString(): String` - String representation of the array (implements `IStringConvertible`)
123+
* `IsLess(other: Object): Bool` - Lexicographic comparison of array elements (implements `IComparable`)
124+
* `GetHash(): Int` - Hash code based on array contents (implements `IHashable`)
125+
126+
### Array Usage
127+
128+
```ovum
129+
// Creating and using arrays
130+
val numbers: IntArray = IntArray(3)
131+
numbers[0] = 10
132+
numbers[1] = 20
133+
numbers[2] = 30
134+
135+
val count: Int = numbers.Length() // Returns 3
136+
137+
// Iteration
138+
for (num in numbers) {
139+
sys::Print(num.ToString())
140+
}
141+
142+
// Array comparison
143+
val arr1: IntArray = IntArray(2)
144+
arr1[0] = 1
145+
arr1[1] = 2
146+
147+
val arr2: IntArray = IntArray(2)
148+
arr2[0] = 1
149+
arr2[1] = 3
150+
151+
val isLess: Bool = arr1.IsLess(arr2) // true (lexicographic comparison)
152+
153+
// Array hashing
154+
val hash: Int = numbers.GetHash()
155+
156+
// String array (used in Main)
157+
fun Main(args: StringArray): Int {
158+
for (arg in args) {
159+
sys::Print("Argument: " + arg)
160+
}
161+
return 0
162+
}
163+
```
164+
165+
## Built-in Interfaces
166+
167+
All types in Ovum implicitly implement certain standard interfaces that provide common functionality.
168+
169+
### Object (Root Interface)
170+
171+
The implicit root interface for all types. Provides:
172+
* `destructor(): Void` - Virtual destructor called by GC during finalization
173+
174+
### IStringConvertible
175+
176+
Provides string conversion capability:
177+
* `ToString(): String` - Converts the object to its string representation
178+
179+
All built-in types implement this interface:
180+
```ovum
181+
val num: Int = 42
182+
val str: String = num.ToString() // "42"
183+
184+
val flag: Bool = true
185+
val flagStr: String = flag.ToString() // "true"
186+
```
187+
188+
### IComparable
189+
190+
Provides ordering capability for sorting and comparison:
191+
* `IsLess(other: Object): Bool` - Returns true if this object is less than the other
192+
193+
Required for user-defined types used as parameters to pure functions (ensures stable ordering).
194+
195+
```ovum
196+
val a: Int = 5
197+
val b: Int = 10
198+
val isLess: Bool = a.IsLess(b) // true
199+
```
200+
201+
### IHashable
202+
203+
Provides hashing capability for use in hash tables and caching:
204+
* `GetHash(): Int` - Returns a hash code for the object
205+
206+
```ovum
207+
val text: String = "Hello"
208+
val hash: Int = text.GetHash()
209+
```
210+
211+
## Type Hierarchy
212+
213+
```
214+
Object (implicit root)
215+
├── IStringConvertible
216+
├── IComparable
217+
└── IHashable
218+
219+
Built-in Types:
220+
├── String (implements all interfaces)
221+
├── File (implements all interfaces)
222+
├── IntArray, FloatArray, etc. (implements all interfaces)
223+
├── Int, Float, Bool, Char, Byte (implements all interfaces)
224+
└── Int?, String?, File?, etc. (nullable versions, implements all interfaces)
225+
```

docs/reference/nullable.md

Lines changed: 115 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,117 @@
11
# Nullability (Kotlin-style)
22

3-
* Append `?` to make a type **nullable**: `Int?`, `String?`, `Point?`.
4-
* **Safe call**: `expr?.Method()` calls only if `expr != null`; otherwise yields `null` (if the method returns a reference type) or a sensible default for chaining to Elvis.
5-
Example: `name?.ToString()?.Length() ?: 0`
6-
* **Elvis**: `a ?: b` evaluates to `a` if non-null, else `b`.
7-
* **Non-null assertion**: `x!!` throws an unhandleable error if `x == null`.
8-
* **Cast to Bool**: any value can be explicitly cast to `Bool`.
9-
10-
* Primitives: zero → `false`, non-zero → `true`.
11-
* Non-primitives: `true` iff the reference is a valid (non-null, live) object.
3+
This document describes how nullable types work in Ovum, including their restrictions and available operations.
4+
5+
## Creating Nullable Types
6+
7+
Append `?` to make a type **nullable**: `Int?`, `String?`, `Point?`. Nullable types are passed by reference and can hold either a value or `null`.
8+
9+
```ovum
10+
val nullableInt: Int? = null
11+
val nullableString: String? = "Hello"
12+
val nullablePoint: Point? = Point(10, 20)
13+
```
14+
15+
## Method Call Restrictions
16+
17+
**Important**: You cannot directly call methods on nullable types using `.` - you must use the safe call operator `?.` or non-null assertion `!!`.
18+
19+
```ovum
20+
val nullableString: String? = "Hello"
21+
// val length: Int = nullableString.Length() // ERROR: Cannot call method directly on nullable
22+
val safeLength: Int = nullableString?.Length() ?: 0 // Correct: Use safe call
23+
val forcedLength: Int = nullableString!!.Length() // Correct: Use non-null assertion
24+
```
25+
26+
## Null Handling Operators
27+
28+
### Safe Call (`?.`)
29+
30+
`expr?.Method()` calls only if `expr != null`; otherwise yields `null` (if the method returns a reference type) or a sensible default for chaining to Elvis.
31+
32+
```ovum
33+
val name: String? = null
34+
val length: Int = name?.Length() ?: 0 // Returns 0 if name is null
35+
val upper: String? = name?.ToUpper() // Returns null if name is null
36+
```
37+
38+
### Elvis Operator (`?:`)
39+
40+
`a ?: b` evaluates to `a` if non-null, else `b`.
41+
42+
```ovum
43+
val nullableInt: Int? = null
44+
val defaultValue: Int = nullableInt ?: 42 // Uses 42 if nullableInt is null
45+
46+
val nullableString: String? = null
47+
val result: String = nullableString ?: "default" // Uses "default" if nullableString is null
48+
```
49+
50+
### Non-null Assertion (`!!`)
51+
52+
`x!!` throws an unhandleable error if `x == null`. Use with caution - only when you're certain the value is not null.
53+
54+
```ovum
55+
val nullableInt: Int? = 42
56+
val mustExist: Int = nullableInt!! // Safe - nullableInt is not null
57+
58+
// val crash: Int = (null as Int?)!! // ERROR: Will abort the program
59+
```
60+
61+
## Type Casting
62+
63+
### Cast to Bool
64+
65+
Any value can be explicitly cast to `Bool`:
66+
67+
* **Primitives**: zero → `false`, non-zero → `true`
68+
* **Non-primitives**: `true` iff the reference is a valid (non-null, live) object
69+
70+
```ovum
71+
val nullableInt: Int? = null
72+
val isNull: Bool = (nullableInt as Bool) // false (null is falsy)
73+
74+
val someInt: Int? = 42
75+
val isNotNull: Bool = (someInt as Bool) // true (non-null is truthy)
76+
```
77+
78+
## Chaining Operations
79+
80+
You can chain safe calls and Elvis operators for complex null handling:
81+
82+
```ovum
83+
val person: Person? = getPerson()
84+
val nameLength: Int = person?.Name?.Length() ?: 0
85+
86+
// Equivalent to:
87+
val nameLength: Int = if (person != null && person.Name != null) {
88+
person.Name.Length()
89+
} else {
90+
0
91+
}
92+
```
93+
94+
## Nullable Type Methods
95+
96+
All nullable types support the same operators but cannot directly call methods:
97+
98+
```ovum
99+
val nullableString: String? = "Hello"
100+
val nullableInt: Int? = 42
101+
102+
// Safe operations
103+
val safeLength: Int = nullableString?.Length() ?: 0
104+
val safeToString: String = nullableInt?.ToString() ?: "null"
105+
106+
// Unsafe operations (will crash if null)
107+
val forcedLength: Int = nullableString!!.Length()
108+
val forcedToString: String = nullableInt!!.ToString()
109+
```
110+
111+
## Best Practices
112+
113+
1. **Prefer safe calls** over non-null assertions when possible
114+
2. **Use Elvis operator** to provide sensible defaults
115+
3. **Avoid non-null assertions** unless you're certain the value exists
116+
4. **Chain operations** for cleaner null handling code
117+
5. **Consider using `if` statements** for complex null checks instead of deeply nested safe calls

0 commit comments

Comments
 (0)