Dynamic language

Adam D. Ruppe destructionator at gmail.com
Thu Mar 15 10:17:24 PDT 2012


On Thursday, 15 March 2012 at 07:09:39 UTC, so wrote:
> I want to invest some "quality" time on a dynamic language but 
> i am not sure which one. Would you please suggest one?

D!

I'm not kidding... let's look at a list of things
you might want:

* You mentioned lisp. One of the cooler things there
   are macros. (And I like the syntax too. It is weird,
   but elegant.)

You can go hog wild with lisp macros, generating all
kinds of new things.

In D, you can do similar metaprogramming with
templates and mixins. The syntax is different, of course,
but you can do much of the same stuff.

* Modifying builtin types and methods. In D, you can
achieve this with classes, UFCS, or alias this.

Javascript might do:

String.prototype.awesome = function() { return 
this.toUpperCase(); }

Then, you can call it like so:

"cool".awesome(); // returns "COOL"


In D, of course, you can just write:

string awesome(string s) {
     return toUpper(s);
}

"cool".awesome(); // returns "COOL"



The newest dmd extends this to more types than just arrays.



What about an integer that subtracts instead of adds?

struct Int {
    int payload;
    alias payload this;

    this(int a) { payload = a; }

    Int opBinary(string op : "+")(int rhs) {
        return Int(payload - rhs);
    }
}


Now use Int instead of int in your code:


void main() {
	Int a = 10;
	assert(a + 5 == 5);
}

and you can pass it to things that expect int, though
you won't get the magic behavior there.


* You might want to replace methods on a function that
doesn't expect it.


This is where we substitute our behavior in code that
doesn't expect it at all... and that's exactly what
class method overriding does.


* You want to get properties or behavior from some outside
   source.

This is where you might use something like Variant, or string
conversions depending on how you got the data.


Javascript:

var a = JSON.parse("[1,2,3]");
assert(a[0] == 1); // pretend js has assert...

D:

auto a = JSON.parse("[1,2,3]"); // see Robert Jacques std.json 
and std.variant, though the version I have doesn't compile right 
with new phobos, it can be fixed
assert(a[0] == 1);


* You want to build objects and properties on the fly.


Javascript:

var a = {};
a.sweet = 10;
a.cool = function() { this.sweet++; }
a.cool();
assert(a.sweet == 11);

D:

import std.variant;
struct Extendable {
     Variant[string] properties;
     Variant opDispatch(string name)() {
        return properties[name];
     }
     Variant opDispatch(string name, T)(T t) {
        properties[name] = t;
        return properties[name];
     }
}

void main() {
     auto a = Extendable();
     a.sweet = 10;
     a.cool = { a.sweet = a.sweet + 1; } // could probably fix ++ 
too..
     // fails with std.variant but the language *could* do it
     a.cool(); // exercise for the reader: maek this work! Gotta 
add opCall.
}





But, yeah, you can seriously use D to explore dynamic programming
as well as programmable programming languages if you want to.

It checks most the big boxes.


More information about the Digitalmars-d mailing list