Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is D powerful enough for these features?

For the longest time I wanted to design a programming language that married extensibility with efficiency (and safety, ease-of-use, etc.) I recently rediscovered D and I am wondering if D 2.0 is pretty much the language I wanted to make myself. What I love most is the potential of metaprogramming; in theory, could D's traits system enable the following features at compile time?

  1. Run-time reflection: Are the compile-time reflection features sufficient to build a run-time reflection system a la Java/.NET?

  2. Code conversion: Using a metaprogram, create C#/C++/etc. versions of your D program every time you compile it (bonus point if doc comments can be propagated).

  3. Traits. I don't mean the metaprogramming traits built into D, I mean object-oriented traits for class composition. A D program would indicate a set of traits to compose, and a metaprogram would compose them.

  4. Unit inference engine: Given some notation for optionally indicating units, e.g. unit(value), could a D metaprogram examine the following code, infer the correct units, and issue an error message on the last line? (I wrote such a thing for boo so I can assure you this is possible in general, program-wide):

    auto mass = kg(2.0);
    auto accel = 1.0;                      // units are strictly optional
    auto force = mass*accel;
    accel += metresPerSecondSquared(9.81); // units of 'force' and 'accel' are now known
    force += pounds(3.0);                  // unit mismatch detected
    
like image 420
Qwertie Avatar asked Jun 09 '12 16:06

Qwertie


People also ask

What are the 3 most important characteristics of a leader?

Passion, teamwork, and social skills are three important qualities for leaders to possess in order to be effective. Learn how to bring these qualities into your workplace and put your best foot forward in both your personal and professional life.

What makes you an effective leader?

As well as providing direction, inspiration, and guidance, good leaders exhibit courage, passion, confidence, commitment, and ambition. They nurture the strengths and talents of their people and build teams committed to achieving common goals.


1 Answers

Run-time reflection: Are the compile-time reflection features sufficient to build a run-time reflection system a la Java/.NET?

Yes. You can get all the information you need at compile time using __traits and produce the runtime data structures you need for runtime reflection.

Code conversion: Using a metaprogram, create C#/C++/etc. versions of your D program every time you compile it (bonus point if doc comments can be propagated).

No, it simply isn't possible no matter how powerful D is. Some features simply do not transfer over. For example, D has an inline assembler, which is 100% impossible to convert to C#. No language can losslessly convert to all other languages.

Traits. I don't mean the metaprogramming traits built into D, I mean object-oriented traits for class composition. A D program would indicate a set of traits to compose, and a metaprogram would compose them.

You can use template mixins for this, although they don't provide method exclusion.

Unit inference engine: Given some notation for optionally indicating units, e.g. unit(value), could a D metaprogram examine the following code, infer the correct units, and issue an error message on the last line? (I wrote such a thing for boo so I can assure you this is possible in general, program-wide):

Yes, this is straightforward in D. There's at least one implementation already.

like image 72
Peter Alexander Avatar answered Oct 01 '22 00:10

Peter Alexander