Example of Rust code

Artur Skawina art.08.09 at gmail.com
Sat Aug 11 06:02:25 PDT 2012


On 08/10/12 14:32, bearophile wrote:
> (Repost from D.learn.)
> 
> Through Reddit I've found a page that shows a small example of Rust code:
> 
> http://www.reddit.com/r/programming/comments/xyfqg/playing_with_rust/
> https://gist.github.com/3299083
> 
> The code:
> https://gist.github.com/3307450
> 
> -----------------------------
> 
> So I've tried to translate this first part of the Rust code to D (I have not run it, but it looks correct):
> 
> 
> enum expr {
>     val(int),
>     plus(&expr, &expr),
>     minus(&expr, &expr)
> }
> 
> fn eval(e: &expr) -> int {
>     alt *e {
>       val(i) => i,
>       plus(a, b) => eval(a) + eval(b),
>       minus(a, b) => eval(a) - eval(b)
>     }
> }
> 
> fn main() {
>     let x = eval(
>         &minus(&val(5),
>                &plus(&val(3), &val(1))));
> 
>     io::println(#fmt("val: %i", x));
> }
> 

Ugh. Haven't really read that article, but how about this
D version:

   import std.stdio;

   template ALIAS(alias A) { alias A ALIAS; }

   static struct Expr(string EVAL, A...) {
      A a;
      static if (is(typeof(a[0].eval)))
         @property a0() { return a[0].eval; }
      else
         alias ALIAS!(a[0]) a0;
      static if (is(typeof(a[1]))) {
         static if (is(typeof(a[1].eval)))
            @property a1() { return a[1].eval; }
         else
            alias ALIAS!(a[1]) a1;
      }
      @property auto eval() {
         static if (is(typeof(mixin(EVAL))))
            return mixin(EVAL);
         else
            mixin(EVAL);
      }
      //alias eval this; // Uncommenting this line will enable automatic
                         // evaluation -- which may not always be desirable.

      auto opBinary(string op, B)(B b) {
         return Expr!("a0" ~ op ~ "a1", Expr, B)(this, b);
      }
   }

   auto Val(V)(V v) { return Expr!("a0", V)(v); }

   void main() {
      auto r = Val(5) - (Val(3) + Val(1));
      writeln("r: ", r, " == ", r.eval);
      auto s = sqr(Val(5) * Val(2) ^^ Val(3));
      writeln("s: ", s, " == ", s.eval);
   }

   auto sqr(T)(T a) { return Expr!("a0*a0", T)(a); }

which is more readable while being much more powerful.

But still trivial enough that the compiler (GDC) evaluates it all
at compile time, even without being asked to do so.

artur


More information about the Digitalmars-d mailing list