Onion is an object-oriented and statically typed programming language. Source codes of Onion compiles into JVM class files as in-memory or real files.
Originally, Onion was written in Java. It has been rewritten in Scala completely except Parser, using JavaCC.
Local val / var declarations can omit types when an initializer is present, and control-flow constructs such as if are expressions.
val name = "Onion"
val label = if name.length > 0 { "ok" } else { "empty" }
def add(x: Int, y: Int): Int = x + y
val double: Int -> Int = (x) -> { return x * 2; }
val inc = (x: Int) -> { return x + 1; }
IO::println(double(21))
Onion supports Haskell-style do notation for composing monadic operations (Option, Result, Future, etc.):
// Async computation with do notation
val result: Future[Int] = do[Future] {
x <- Future::async(() -> { return fetchUser(); })
y <- Future::async(() -> { return fetchData(x); })
ret x + y
}
// Option chaining
val user: Option[String] = do[Option] {
id <- lookupId("alice")
profile <- loadProfile(id)
ret profile.name
}
Methods accepting a function as the last parameter can use trailing lambda syntax:
// Traditional call
list.map((x: Int) -> { return x * 2; })
// With trailing lambda
list.map { x => x * 2 }
// Multiple arguments + trailing lambda
future.onComplete(onSuccess, onFailure) { result =>
IO::println("Done: " + result)
}
Built-in Future[T] type for async operations:
val future: Future[String] = Future::async(() -> {
return Http::get("https://api.example.com/data");
})
future.map((data: String) -> { return parseJson(data); })
.onSuccess((result: Object) -> { IO::println(result); })
.onFailure((error: Throwable) -> { IO::println("Error: " + error); })
The compiler parses source code into an untyped AST and then performs type checking to produce a typed AST. The old intermediate representation (IRT) has been folded into this typed tree. Code generation now runs on the typed AST via a thin compatibility layer using ASM.
onionc [options] source files...- -classpath Set classpath of source files in compilation.
- -encoding Set encoding of source files.
- -d Set output directory of results.
- -maxErrorReports Set the maximum number of comiplation errors reported.
- --dump-ast Print parsed AST to stderr.
- --dump-typed-ast Print typed AST summary to stderr.
- --warn <off|on|error> Set warning level.
- --Wno Suppress warnings (e.g., W0001,unused-parameter).
onionc compiles source files into class files in the directorys corresponding to module names
of source files rooted by "-d" option. If "-d" is not specified, the value of "-d" is specified as the current directory.
For example, if source files which module name is "org.onion_lang" is compiled, class files are generated under:
- Unix-like OS : org/onion_lang
- Windows: org\onion_lang
onion [options] source files... [command line arguments]
- -classpath classpath of source files in compilation.
- -encoding encoding of source files.
- -maxErrorReports the maximum number of comiplation errors reported.
- --dump-ast Print parsed AST to stderr.
- --dump-typed-ast Print typed AST summary to stderr.
- --warn <off|on|error> Set warning level.
- --Wno Suppress warnings (e.g., W0001,unused-parameter).
onion compiles source files into in-memory class files and execute them. The entry point is:
- A main method if there is an explicit class definition and it have the main method.
- The main method of the class on the top.
- Otherwise, the first statement on the top.
- The compiler still has edge cases and can crash; please report minimal repros.
- Generics are erasure-based (no variance, wildcards, or reified type info).
- Diagnostics are still improving; some errors may be reported later in the pipeline.