Book Image

D Cookbook

By : Adam Ruppe
Book Image

D Cookbook

By: Adam Ruppe

Overview of this book

Table of Contents (21 chapters)
D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a command-line function caller


One useful application of reflection is that it builds dynamic interfaces automatically. Reflection can be used to help the code that interacts with a scripting language, a network protocol, and more. Here, we'll write a command-line program that calls functions and displays information about them when help is requested. We'll need to be able to add new functions without needing any boilerplate code to be added along with it.

How to do it…

We can create a command-line function caller by performing the following steps:

  1. Loop over all members of the module.

  2. Find functions with a protection level of export (alternatively, you may look for the presence of a user-defined attribute).

  3. If the user requested help, list the possible functions or details about a particular function, by performing the following steps:

    1. Use a user-defined attribute for documentation.

    2. Use std.traits' Parameter*Tuple family of functions to get details about the function.

  4. If the user wants...