Code Samples

Elide is a single binary that is two things at once: a polyglot runtime (JavaScript, TypeScript, Python, and the JVM) and a complete toolchain (compilers, a code formatter, a package installer, and a server). Every example below uses only that one binary -- there is no Node, no python, no JDK, and no build tool to install alongside it.

How to read this page

Each section states a real problem, shows the code, shows the exact command, and shows the output. If you only have five minutes, read the formatter section -- it is the feature most teams reach for first.

Run any language, no build step

Elide picks a language from the file extension. There is nothing to configure and nothing to compile first.

ts
// discount.ts
interface Order {
  item: string;
  price: number;
  qty: number;
}

function total(order: Order): number {
  return order.price * order.qty;
}

const order: Order = { item: "keyboard", price: 79.99, qty: 2 };
console.log(`${order.item}: ${total(order).toFixed(2)}`);
bash
 elide discount.ts
terminaloutput
keyboard: $159.98

That file is TypeScript with real type annotations, and it ran directly. Elide strips the types at native speed (via OXC) and executes the result -- there is no tsconfig.json and no transpile step. The same is true for .js, .mjs, and .py files.

Because the runtime is introspectable, you can ask Elide about itself from guest code through the global Elide object:

js
// about.js
console.log("Elide", Elide.version);
console.log("- build mode:", Elide.buildMode);
console.log("- target:", Elide.targetArchitecture);
bash
 elide about.js

See Getting Started for the language-detection rules, and Elide + Web for the JS/TS runtime details.

Format Java and Kotlin with one command

This is Elide's most immediately useful tool. elide fmt (aliased as elide format) is a single, unified formatter for both Java and Kotlin. It embeds Google Java Format and ktfmt, compiled ahead-of-time with native image, so it starts in milliseconds instead of paying JVM warm-up on every invocation.

You point it at files or directories; it classifies each source by extension (.java to Google Java Format, .kt / .kts to ktfmt) and formats accordingly. Everything after the -- separator is passed through to the formatters.

Fix files in place

Take a messily written Kotlin file:

kotlin
// Greeter.kt
package sample

object   Greeter {
fun greet(name: String): String
{
      return "Hello, ${name}!"
}
}
bash
 elide fmt -- Greeter.kt
terminaloutput
Formatted 1 Kotlin sources
kotlin
// Greeter.kt
package sample

object Greeter {
  fun greet(name: String): String {
    return "Hello, ${name}!"
  }
}

Kotlin is rewritten in place by default. Java is different in one important way: Java files are only written back when you pass -i (or --replace) -- without it, the Java engine runs but leaves the file untouched:

java
// Message.java
public class Message {
public static String greet(String name){
return "Hello, "+name+"!";}
}
bash
 elide fmt -- -i Message.java
java
// Message.java
public class Message {
  public static String greet(String name) {
    return "Hello, " + name + "!";
  }
}

A mixed project is a single command -- -i applies to the Java files, and Kotlin is rewritten in place regardless:

bash
 elide fmt -- -i src
terminaloutput
Formatted 1 Java sources
Formatted 1 Kotlin sources

Check formatting in CI (without changing files)

The pair every CI pipeline wants is --dry-run (report, don't write) and --set-exit-if-changed (exit non-zero if anything is out of format). Both flags apply to Java and Kotlin at once:

bash
 elide fmt -- --dry-run --set-exit -if -changed src
console
$ elide fmt -- --dry-run --set-exit-if-changed src
src/Message.java
Format-checking failed after 1 Java sources
Format-checked 1 Kotlin sources
$ echo $?
1

An exit code of 1 fails the build; a clean tree exits 0. That single line replaces a Java formatter check and a Kotlin formatter check with one call.

Zero-config, whole-project formatting

If you run the formatter with no file arguments, it reads the sources globs from your elide.pkl project manifest and formats exactly the files your project already declares:

bash
# Format-check everything the project declares as source:
 elide fmt -- --dry-run --set-exit -if -changed

There is no separate formatter config file to maintain -- style is controlled by flags, and the file set comes from the project you already defined.

Choose a style

Both formatters support their standard style options:

bash
# Java: AOSP style (4-space indent) instead of the Google default
 elide fmt -- --aosp -i Message.java

# Kotlin: Google internal style (2 spaces) or Kotlin-lang style (4 spaces)
 elide fmt -- --google-style Greeter.kt
 elide fmt -- --kotlinlang-style Greeter.kt

Surgical edits

The Java engine can tidy imports without touching anything else, or format only a line range -- useful for large files and pre-commit hooks:

bash
# Sort imports and drop unused ones, nothing else:
 elide fmt -- --fix-imports-only -i Message.java

# Format only lines 10 through 25:
 elide fmt -- --lines 10:25 -i Message.java

Flag reference

FlagApplies toEffect
-i, -r, —replaceJavaWrite result back to the file (Java files are not modified without it).
—dry-run, -nJava + KotlinReport files that would change; do not write.
—set-exit-if-changedJava + KotlinExit 1 if any file is not already formatted.
—aospJavaUse AOSP style (4-space indent).
—fix-imports-onlyJavaOnly sort/remove imports.
—skip-removing-unused-importsJavaKeep unused imports.
—lines, —offset, —lengthJavaRestrict formatting to a range.
—meta-style / —google-style / —kotlinlang-styleKotlinSelect Kotlin style (Meta is the default).
—do-not-remove-unused-importsKotlinKeep unused imports.
Language-specific tools

If you only need one language, the engines are also exposed directly as elide javaformat -- ... and elide ktfmt -- .... See Format Java and Kotlin Formatting.

Call JavaScript from Python

Elide runs every language on one engine, so values cross language boundaries without serialization. From Python, the built-in elide module lets you evaluate JavaScript and export Python functions into the shared polyglot bindings:

python
# interop.py
from elide import bind, js, poly

# Python -> JS: evaluate JavaScript and use the result back in Python.
total = js("[1, 2, 3, 4].reduce((a, b) => a + b, 0)")
print(f"js sum = {total}")

# Python -> host: export functions for JavaScript / the host to call.
@bind
def add(a, b):
    return a + b

@poly(name="multiply")
def mul(a, b):
    return a * b

print("interop ok")
bash
 elide interop.py
terminaloutput
js sum = 10
interop ok
js(source) evaluates a JavaScript string and hands the result back as a native Python value; bind and poly publish Python callables under a name that other languages can look up. See Elide + Python for the full interop surface.

Use SQLite with zero dependencies

A database driver is a classic "install three packages first" task. In Elide, SQLite is a built-in module -- import it and go:

ts
// db.ts
import { Database } from "elide:sqlite";

const db = new Database(); // in-memory
db.exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);");
db.exec("INSERT INTO users (name) VALUES ('Ada'), ('Alan');");

const row = db.query("SELECT name FROM users ORDER BY id LIMIT 1;").get() as {
  name: string;
};
console.log(`first user: ${row.name}`);
bash
 elide db.ts
terminaloutput
first user: Ada

No npm install, no native build, no lockfile -- the driver ships inside the runtime.

One project, three ecosystems

Real projects are usually already polyglot. Elide's manifest, elide.pkl, declares npm, PyPI, and Maven dependencies together, and a single elide install resolves all three with in-process resolvers -- no npm, pip, or mvn to install separately.

pkl
amends "elide:project.pkl"

name = "polyglot-app"

dependencies {
  maven {
    packages {
      "org.slf4j:slf4j-api:2.0.17"
    }
  }
  npm {
    packages {
      "pluralize@8.0.0"
    }
  }
  pypi {
    packages {
      "six==1.17.0"
    }
  }
}
console
$ elide install
NPM packages installed (412ms)
+ pluralize@8.0.0
Python dependencies resolved (3ms)
Python packages installed (1ms)
Project dependencies installed in 513ms
Resolved 13 Maven dependencies (106ms)
Dependencies installed in 157ms

Everything lands under .dev/dependencies/, with conventional layouts (a root node_modules symlink, a site-packages directory, a project-local Maven repository under .dev/dependencies/m2) so existing tools still work. Imports resolve against those installed packages with no further setup:

ts
// main.ts
import pluralize from "pluralize";
console.log(pluralize("dependency", 3, true));
console
$ elide run main.ts
3 dependencies

Build and test with the same binary:

bash
 elide build      # compile the project
 elide test       # run tests

See Dependencies for the resolver internals and Elide Projects for the full manifest schema.

Serve and iterate

Elide has an HTTP server built in. Serve a directory of static files, or use the dev server for live-reload while you work:

bash
 elide serve ./public                 # static file server (port 3000 by default)
 elide serve --host 0.0.0.0 --port 8080 ./dist
 elide dev ./public                   # live-reload + file watching
terminaloutput
Serving static files on 127.0.0.1:3000

Both commands render an interactive terminal dashboard by default; pass --no-tui for plain log output in CI. The full flag list is in the CLI Reference.

Where to go next