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 struct with reference semantics


D structs are typically a value type (different variables are always different objects), as opposed to D's classes, which are always a reference type (different variables may refer to the same object). Sometimes, we want the struct that has reference semantics to ensure that assignment is cheap, for example.

How to do it…

To create a struct with reference semantics, we will execute the following steps:

  1. Create a struct with a single data member that is itself a reference type, such as a pointer.

  2. You may choose to use a private nested struct to hold the implementation, using alias this to forward methods automatically. The reference counted object in the previous chapter is a reference-type struct that uses this technique.

  3. Use the struct normally—you should not use the ref storage class when passing it to functions.

How it works…

Structs are a compile-time collection of their contents. If a struct has only one member, it works almost exactly the same way...