Chapter 5: Object-Oriented Programming - Building Complex Systems
For larger programs, you need to organize your data and behavior into cohesive units. That's where classes come in.
Defining a Class
A class is a blueprint for creating objects. It combines data (fields) and behavior (methods).
class Spaceship:
val name: string
var fuel: float
var speed: float
def __init__(self, ship_name: string, initial_fuel: float) -> void:
this.name = ship_name
this.fuel = initial_fuel
this.speed = 0.0
def accelerate(self, amount: float) -> void:
if this.fuel > 0.0:
this.speed = this.speed + amount
this.fuel = this.fuel - (amount * 0.1)
else:
print("Out of fuel!")
def status(self) -> string:
return f"{this.name}: Speed={this.speed}, Fuel={this.fuel}"
Creating and Using Objects
val my_ship: Spaceship = new Spaceship("Phoenix", 100.0)
my_ship.accelerate(50.0)
print(my_ship.status())
The this Keyword
Inside a method, this refers to the current instance. Use it to access fields and methods of the object.
Public vs. Private (Simplified)
In REDLINE, all members are public by default. For production systems, you can use the pub keyword explicitly to indicate intent, though it has no effect on visibility in the current version.
Chapter 6: The Standard Library - Essential Tools
REDLINE ships with a carefully curated standard library. Each module is a .hpp C++ header that gets included automatically in the generated code.
System & Utilities (rl_system.hpp)
args -> list[string]: Returns the command-line arguments passed to the program.len(collection): Returns the size of a list or dictionary.append(list, value),sort(list),reverse(list),find(list, value): Collection operations.to_string(value) -> string,to_int(string) -> int,to_float(string) -> float: Type conversions.
I/O (rl_io.hpp)
print(value): Prints a value to the console, followed by a newline.input(prompt: string) -> string: Displays a prompt and reads a line of text from the user.
File System (rl_file.hpp)
read_file(path: string) -> string: Reads an entire file into a single string. Throws an error if the file can't be opened.write_file(path: string, content: string): Writes a string to a file, overwriting it if it exists.exists(path: string) -> bool: Returnstrueif a file or directory exists.mkdir(path: string): Creates a new directory.remove(path: string): Deletes a file or an empty directory.list_dir(path: string) -> list[string]: Returns a list of filenames in a directory.
Time (rl_time.hpp)
time() -> float: Returns the current system time as a Unix timestamp (seconds since 1970).sleep(seconds: float): Pauses the program's execution for the given number of seconds.
Random (rl_random.hpp)
random_int(min: int, max: int) -> int: Returns a random integer betweenminandmax(inclusive).random_float() -> float: Returns a random float between 0.0 and 1.0.
Strings & F-Strings
For complex string formatting, f-strings are the best tool for the job. Any valid REDLINE expression can be placed inside the curly braces.
val name: string = "Redline"
print(f"The current time is {time()}.")
print(f"A random number: {random_int(1, 100)}.")