Get the code of any D-entity as string?

Max Haughton maxhaton at gmail.com
Sun Dec 27 04:13:53 UTC 2020


On Friday, 25 December 2020 at 21:25:40 UTC, sighoya wrote:
> I've read a bit in Dlang traits.
>
> It now has the ability to retrieve all method signatures of an 
> overload set.
> Big plus from me.
>
> Is generally possible to get the declaration of a 
> type/module/value as string in traits?
>
> I didn't have any concrete use case for it, but it would 
> essentially allow us to reflect over any code (we may have to 
> respect privacy, of course).
>
> On top of that, people could write their own token or ast 
> frameworks as library solutions.
>
> Further, most of the trait functionality given in the trait 
> library could be simply implemented as a library solution.
>
> I don't know D enough to be sure it isn't yet possible. 
> Theoretically, we could read all files in a source folder, but 
> I want also to read declarations from types outside my source 
> folders, e.g. from those located in dyn libs.

Not possible although implementing as a __trait would be about 15 
lines I think.

I think read only AST access in some form would actually be quite 
nice, if not for dmd's AST being fuckugly (the hierarchy is fine 
but it's more gcc than clang)


More information about the Digitalmars-d-learn mailing list