Host interop
Importing
Section titled “Importing”import: in the ns form gives a host class an alias:
ns: example import: java.time: as(Instant, I) java.util: ArrayListThe 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).
Declaring members
Section titled “Declaring members”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() Idecl: I/parse(Str) Idecl: I/.toEpochMilli() Intdecl: I/.isAfter(I) Bool
decl: [a] ArrayList/new() ArrayList(a)decl: [a] ArrayList/.add(a) BoolHost 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?.
Calling
Section titled “Calling”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.
Exceptions
Section titled “Exceptions”A host exception is caught as the Host anomaly:
try: java.lang.Integer/parseInt(s) catch: Host(e) 0Collections
Section titled “Collections”Bridje vectors and sets are Iterable, and so are Java Iterables: mapv, filterv and reduce work over a Java List directly.
Other GraalVM languages
Section titled “Other GraalVM languages”lang(language, Type, code)- Evaluates
codein another installed GraalVM language, taking its result to be aType:lang("js", Int, "6 * 7")languageandcodemust be string literals, and the language must be on the classpath alongside Bridje.
Bridje values from outside
Section titled “Bridje values from outside”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.