Can you do this in D?

bearophile bearophileHUGS at lycos.com
Thu Jul 26 04:44:20 PDT 2012


Wes:

> Can you do this in D? If not then what other language?

Maybe CommonLisp and Scheme (and maybe Clojure) are able to do 
what you ask for (but normally they don't have the 
stringification with $).


> 1. Can you get a list of all classes and variables in the 
> current scope e.g. __traits()?

I think it's a missing feature. But what are the use cases? In 
Python it's easy to do, but it's not a common need.


> 2. Can you statically iterate over every field of a class?
> It seems like the only returns are string lists from __traits().
> I basically am interested in being able to generate a
> PrettyPrint/Serializable/Hash template without passing in every
> single field.

With their names and mixin() then maybe you able to do what you 
look for.


> 3. Is there any way of executing code or programs during compile
> time?

Only CTFE.


> I've seen an example of CTFE (Compile Time Function Evaluation),
> although I'm unsure if this works for stuff like classes.

CTFE now works with classes too.


> However, I am considering more advanced execution (not 
> constants)
> such as printing to a file during compiling for stuff like how
> long compiling a certain function/template takes.

With the proposed __ctWrtiteln you are able to print things at 
compile time to standard output. But there is no enough 
introspection to know how much time it takes to compile part of a 
program.


> 5. Why not support other operators like $, #, and @?
> This is more of a rhetorical... as I know the language doesn't
> need them, nor would I know if they would be binary/unary
> prefix/etc or the precedence... although they would be nice to
> have. Specifically I'd like $prefix to be stringification.

The operator overloading syntax is string-based, so adding new 
operators is not too much hard. But I don't know what are the 
advantages and disadvantages. The $ operator is used (in a 
certain context) to denote the length of collections.

Bye,
bearophile


More information about the Digitalmars-d mailing list