Skip to content

Host interop

import: in the ns form gives a host class an alias:

ns: example
import:
java.time:
as(Instant, I)
java.util:
ArrayList

The alias names both a type — I in a declaration — and a namespace of the class’s members.

A class can also be named in full without importing it: java.lang.Math/abs(-3).

A member’s declaration gives the checker its type; Bridje trusts it.

decl: Alias/method(P1, P2) R
A static method.
decl: Alias/.method(P1, P2) R
An instance method. It’s called with the receiver as its first argument, so its type is Fn([Alias, P1, P2] R).
decl: Alias/FIELD T
A static field.
decl: Alias/.field T
An instance field.
decl: [a] Alias/new() Alias(a)
A constructor, with type parameters.
decl: I/now() I
decl: I/parse(Str) I
decl: I/.toEpochMilli() Int
decl: I/.isAfter(I) Bool
decl: [a] ArrayList/new() ArrayList(a)
decl: [a] ArrayList/.add(a) Bool

Host type arguments are invariant; two host types join to their nearest common superclass. A host class Bridje represents as a primitive is that primitive: an imported String is a Str. A method that returns nothing useful (void) returns nil, so it’s declared Nothing?: decl: T/sleep(Int) Nothing?.

Alias/method(a, b)
Calls a static method. An undeclared static method can still be called, untyped.
Alias/.method(receiver, a, b)
Calls an instance method, or reads an instance field. The member must be declared.
Alias/FIELD
Reads a declared static field.
Alias/new(), Alias(a, b)
Constructs an instance.

A host member is spelled exactly as the host spells it: I/.toEpochMilli, URI/.toURI.

A host exception is caught as the Host anomaly:

try:
java.lang.Integer/parseInt(s)
catch: Host(e) 0

Bridje vectors and sets are Iterable, and so are Java Iterables: mapv, filterv and reduce work over a Java List directly.

lang(language, Type, code)
Evaluates code in another installed GraalVM language, taking its result to be a Type:
lang("js", Int, "6 * 7")
language and code must be string literals, and the language must be on the classpath alongside Bridje.

Bridje values are polyglot values:

  • functions are executable;
  • vectors have array elements;
  • records, and tagged values, have members named after their keys: getMember("name");
  • a namespace, as returned by evaluating a file, has members named after its definitions.