
@leif There are some support for ES6 modules, but the support could be improved.
Unlike JavaScript Urlang will signal an error when an unbound variable is referenced. The form (import id1 id2 ...)
simply tells Urlang that the identifiers in fact are available at runtime. There is no way for Urlang to determine this at compile time, due to the nature of JavaScript.
For example, if you are running the module in a browser, then Promise,
fetch,
Object
will be present.
Also if you in the html for your page use, say a script to load a library <script src="<https://code.jquery.com/jquery-3.3.1.slim.min.js>"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
then the identifier exported by the library (here $
) will be available.

Note that Urlang grew up before ES6 was available in browsers, so I targeted Node/ES5. Now I am slowly adding the features from ES6 that in some way or other need.

Some of the new additions are: <import-from> ::= (import-from <string-literal> <import-spec> ...)
; <import-spec> ::= x \| (as x x) \| (all-as x) \| (default x)
which map to the new ES6 import declarations.

Thus the import side of things ought to work.
But I haven’t tried importing Urlang modules from JavaScript - so that might need some work. If you run into an example not working, let me know.

Also … if you are importing ES6 modules, then take a look at Skypack. The idea behind Skypack is to use ES6 modules without a build step.


@soegaard2 Ah, okay. Although that doesn’t seem to be the case for some primitives, such as + or -

THose seem to just work without an explicit import?

(Despite you putting them in there in your fact
example.)

So, probably a JS question, but is there any explicit purpose to the generated .exports
file?

(I mean, obviously it lists all of the exports, but I’ve never seen it used in JS before)

re skypack, interesting, I’d never realized that es6 modules had a pre-processor step.

I thought browsers were able to interpret their code directly.

But I’m okay with not using ES6 modules specifically, but is there any other module system that works in a browser?

(Or am I going to have to use functions as my main form of code seperations?)

es6 modules do work in current browsers, but are often compiled away because they don’t work in some older browsers


@samth Ah, okay, thanks.

@soegaard2 hmm…it also appears there isn’t really any easy way to add json objects to urlang, unless I’m missing something.

Also, it appears you can’t do something like:

var x = [];
x.a = 42;

When I do:
(define x (array))
(:= x.a 42)
I get an x.a
unbound error.

Additionally this doesn’t work:
(define x (array))
(:= (ref x "a") 42)

In JavaScript + and - are operators, so they are always available.

The “explicit import” is a bit cumbersome, so I had/have plans to add a feature: If an Urlang module B was imported into an Urlang module A, then B.exports could be used to automatically determine the imported identifiers.

The state of JavaScript modules before ES6 was very confusing. There were at least two different sets of conventions (since they weren’t in the ES5 standard). Tools existed to package sets of modules into something easily deployed to a browser.

Assignment to dotted identifiers are written: (:= x "a" 42)
and becomes x["a"]=42
When producing Urlang as the output of a macro, this style is nicer than the dotted style.
However - please add an Github issue saying “support dotted identifiers in assignments”. (I’ll admit, that I thought I already added that feature).

@leif JSON ought to work fine. First (import JSON)
. Then use the methods provided. For example: (JSON.stringify (object [make "toyota"] [color "red"])