|
1 | 1 | # Built-in Reference Types |
2 | 2 |
|
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 | +``` |
0 commit comments