Is this D or is it Javascript?

Adam D. Ruppe destructionator at gmail.com
Thu Jul 4 20:21:18 PDT 2013


import arsd.jsvar;
import std.stdio;

void main() {
   var a = 10;
   var b = a - 5;
   a = [1,2,3];
   a[1] = "two";
   writeln(a);

   a = json!q{
      "foo":{"bar":100},
      "joe":"joesph"
   };

   writeln(a.joe);
   a.bar += 55;
   b = a;
   writeln(b.bar);

   var c = (var d) { writeln("hello, ", d, "!"); };
   c("adr");
   c(100);

}


https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/jsvar.d


Ever since opDispatch came about, I've wondered just how close D 
can get to being like Javascript if we wanted to do that kind of 
thing. Last time I tried this, I hit bugs relating to opCall and 
constructors being mistaken for one another, but those dmd bugs 
have now been fixed.

So I tried again this weekend.... and there's the result. It 
works now! (Only problem left is the @property debacle, which is 
why I made yet another thread on that. But meh, that isn't even a 
big deal.)


The var type is, as you can see, both weakly and dynamically 
typed. Now, I know weak and dynamic typing sucks. But the beauty 
with D is you can use whatever you want. std.variant offers 
strong, dynamic typing. D itself offers strong, static typing. 
And now var offers the weak/dynamic quadrant.

Why would you use this? idk, maybe json, maybe script interaction 
(see below), but I did it more because I can than any real goal.


I just think D rox that it can do all this under one language.



But, if we can do this in D, wouldn't it be nice to interop with 
a scripting language so easily too? I thought so, so Sunday and 
some more today, I slapped together a quick script interpreter 
that uses this var type:

https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/script.d



auto globals = var.emptyObject;
// these global variables will be available to the script
globals.lol = 100;
globals.rofl = 23;

// and functions too
globals.write = (var a) { writeln("script said: ", a); };

// read from file
import std.file;

// this can contain: var c = function(a) { write(a); };
writeln(interpret(readText("scripttest_code.d"), globals));

// or from stdin
repl(globals);

writeln("BACK IN D!");
globals.c()(10); // call script defined functions in D




(I thought about tying into dmdscript, but meh, doing my own 
language let me use the var type without wrapping it, and I could 
play with some other ideas too. I think the idea of using mixins 
to forward script operators to D is potentially very interesting 
- perhaps I could add static typing to the script that depends on 
dmd to do the actual work. But for now all it offers is the var 
type.)


That script language isn't quite complete, I just quickly hacked 
it together to play with. Notably, "return" doesn't actually 
work. But most of the functions and for loop and math stuff do, 
as well as some nice imports from D like exceptions and scope 
guards.


What's most interesting isn't the script itself though, it is how 
easy it can be to integrate it with the rest of a D program, like 
shown above.



Is any of this useful for anything? idk, maybe parsing json or 
maybe the script language could find use. But the real point I'm 
trying to make here is simply that...

d rox.


More information about the Digitalmars-d-announce mailing list