soegaard2
2020-9-27 18:24:41

@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 &lt;script src="<https://code.jquery.com/jquery-3.3.1.slim.min.js>" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"&gt;&lt;/script&gt; then the identifier exported by the library (here $) will be available.


soegaard2
2020-9-27 18:25:48

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.


soegaard2
2020-9-27 18:27:06

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


soegaard2
2020-9-27 18:30:07

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.


soegaard2
2020-9-27 18:36:14

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
2020-9-27 18:36:25

leif
2020-9-27 20:46:01

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


leif
2020-9-27 20:46:20

THose seem to just work without an explicit import?


leif
2020-9-27 20:46:33

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


leif
2020-9-27 20:46:51

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


leif
2020-9-27 20:47:09

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


leif
2020-9-27 20:49:04

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


leif
2020-9-27 20:49:36

I thought browsers were able to interpret their code directly.


leif
2020-9-27 20:50:03

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


leif
2020-9-27 20:50:14

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


samth
2020-9-27 20:52:22

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



leif
2020-9-27 21:04:48

@samth Ah, okay, thanks.


leif
2020-9-27 23:08:40

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


leif
2020-9-27 23:25:44

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


leif
2020-9-27 23:25:51

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


leif
2020-9-27 23:26:52

When I do:

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


leif
2020-9-28 04:07:56

Additionally this doesn’t work:

(define x (array)) (:= (ref x "a") 42)


soegaard2
2020-9-28 05:05:29

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


soegaard2
2020-9-28 05:07:22

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.


soegaard2
2020-9-28 05:10:04

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.


soegaard2
2020-9-28 05:16:05

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).


soegaard2
2020-9-28 05:18:14

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