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

Wrapping instance methods


Earlier, we looked at a dynamic type in D which included the capability to wrap native functions with dynamic typing and property replacement. Now, it is time to see exactly how that works and how automated code generation makes it a lot easier.

Note

By unifying types, we enable both dynamic transformations as well as create an array of delegates to access functions of varying types. We cannot declare an array of function pointers that all take different arguments, but we can declare an array of pointers to helper functions that take one input array and transform the values for use.

How to do it…

Let's execute the following steps to wrap instance methods:

  1. Write a generic conversion function to and from your consistent type. You should use std.variant.Variant or std.conv.to directly, if possible, and use type families with std.traits if working with a custom type.

  2. Write a helper function generator that takes an existing function as a runtime delegate and returns the wrapped...