D-etractions A real world programmers view on D

Jacob Carlborg doob at me.com
Wed Sep 5 23:44:20 PDT 2012


On 2012-09-05 20:22, Nick Sabalausky wrote:

> As far as monkey patching, I find *not* having that to be a major
> feature. :)

It has saved my ass several times. Especially recently when we started 
to update Ruby 1.8 to 1.9.

> While a lot of runtime reflection *can* be built in D (even if it
> isn't all there right now), I am kind of annoyed at some of the
> limitations. Like how it's impossible (at either compile-time or
> runtime) to find the subclasses of a class. Uhh, actually that's the
> only limitation I know of. There might be more though.

Hmm, I actually haven't needed that in D yet. But it's so cool in Ruby, 
you can sort of hook up an event and be notified when a subclass 
inherits from its base class. The subclass then gets passed to the event 
handler and you can add new methods on the subclass.

>> * Have executing code in all level of scopes
>
> Not sure what sure mean by this?

The obvious one is you can have executable code at the global level. The 
less obvious one is you can have executable code when declaring a class:

def bar
end

class Foo
   bar() # call bar
end

This is heavily used in Ruby on Rails' ActiveRecord implementation:

class Post
   has_many :comments
end

class Comment
   belongs_to :post
end

The cool thing is that these are just plain method class (class 
methods). They will add some methods to context where they are called. 
Any other language would probably need annotations or some other kind of 
extra language feature to get this nice looking syntax.

Currently in D you would probably use template mixins:

class Post
{
     mixin hasMany!("comments);
}

class Comment
{
     mixin belongsTo!("post");
}

But I think this use case would be perfect for AST-macros or user 
defined attributes/annotations:

class Post
{
     @hasMany("comments");
}

class Comment
{
     @belongsTo!("post");
}


> Needs polish, but:
>
> https://bitbucket.org/Abscissa/semitwistdtools/src/977820d5dcb0/src/semitwist/util/process.d#cl-49
>
> Usage:
>
> string str;
>
> str = q{ return 42; };
> assert( eval!int(str) == 42 );
>
> str = q{ return "Test string".dup; };
> assert( eval!(char[])(str) == "Test string" );
>
> :)
>
> Granted, there'd probably be a lot less overhead doing that in Ruby or
> JS. But it works, more or less.

I'll have to take a look at that sometime. Is it that code that calls 
RDMD to compile and run it?

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list