Fahrenheit 0.4.0

Standard Library

Standard Library

Available globally without imports.

I/O

FunctionDescription
print(args...)Print to stdout without newline.
println(args...)Print to stdout with newline.
input(prompt)Read input from stdin with prompt.
read()Read line from stdin.

Time & Threading

FunctionDescription
time()Get current time in milliseconds.
sleep(ms)Sleep for ms milliseconds.
settimeout(delay, methodName, [target])Execute a method (or object method) after a delay in milliseconds (non-blocking).

Randomness

FunctionDescription
random()Random double [0.0, 1.0).
randomint(max)Random integer [0, max).

Type Inspection & Conversion

FunctionDescription
typeof(val)Return the type of a value as a string.
reflect(val)Introspect a value (variable, literal, struct, etc.) to get its metadata.
isnumber(val)Check if a value is a number.
isstring(val)Check if a value is a string.
isarray(val)Check if a value is a FahrenheitArray.
ismap(val)Check if a value is a FahrenheitMap.
isbound(val)Check if an object instance is currently bound.
hasfield(instance, fieldName)Check if a struct instance has a specified field.
parseInt(str)Parse a string to an integer.
parseDouble(str)Parse a string to a double.
parseBoolean(str)Parse a string to a boolean.
tostring(val)Convert a value to its string representation.

String Operations

FunctionDescription
length(str)Return the length of a string.
trim(str)Remove leading and trailing whitespace.
tolower(str)Convert string to lower case.
toupper(str)Convert string to upper case.
substring(str, start, [end])Return a substring from start (inclusive) to end (exclusive).
replace(str, old, new)Replace all occurrences of old with new in str.
startswith(str, prefix)Check if str starts with prefix.
endswith(str, suffix)Check if str ends with suffix.
index(str, marker)Return the first index of marker in str, or -1.
split(str, delimiter)Split string into an array.
concatenate(array)Join array elements into a string with no separator.
join(delimiter, array)Join array elements into a string separated by delimiter.
interpolate(str)Interpolate variables in a string using """ +"$"+"""expression syntax.

Environment & Scripting

FunctionDescription
getenv(variable)Get the value of an environment variable.
environment()Get all environment variables as a map.
args()Get the arguments passed to the script as an array.
assert(condition, message, [scriptexception])Assert a condition is true, throwing an exception if false.
call(methodName, args...)Call a method by name using string indirection.

Date and Time

FunctionDescription
timestampToDate(ts)Convert a millisecond timestamp to a Date struct.
dateToTimestamp(d)Convert a Date struct to a millisecond timestamp.
printDate(d)Print a Date struct (YYYY-MM-DD HH:MM:SS).
formatDate(d, fmt)Format a Date struct using a pattern.

Dynamic Method Invocation

The call() function allows calling methods by name using String variable indirection:


function greet(name) {
    return "Hello " + name;
}

methodName = "greet";
result = call(methodName, "World");  # Returns "Hello World"
    

Delayed Execution

The settimeout() function executes a method after a specified delay. Execution is non-blocking and runs in a separate thread. An optional third argument can be an object instance whose method is called, or a callbacks struct with onStart() and onEnd() hooks.


function delayed() {
    println("Executed after 1 second");
}
settimeout(1000, "delayed");

# Execute an object method
structure Worker {
    name;
    function doWork() { print(this.name + " is working"); }
}
w = Worker("Alice");
settimeout(500, "doWork", w);

# With lifecycle callbacks
structure Callbacks { onStart; onEnd; }
function onStart() { println("Task started"); }
function onEnd() { println("Task completed"); }
cb = Callbacks("onStart", "onEnd");
settimeout(2000, "delayed", cb);
    

Date and Time (Example)


# Get current time
t = time();

# Convert to Date struct
d = timestampToDate(t);
println("Year: " + d.year);

# Format date
println(formatDate(d, "yyyy-MM-dd HH:mm"));

# Modify and convert back
d.year = 2025;
futureTime = dateToTimestamp(d);