-
Book Overview & Buying
-
Table Of Contents
D Cookbook
By :
By disabling default construction, we can force initialization of a type. Using this feature, we'll make an object reference that will force the user to check for null before using it.
To create a NotNull struct, perform the following steps:
Create struct NotNull(T) with a private member of type T.
Disable default construction with @disable this();.
Write a private constructor that takes T and assert(t !is null);.
Write a property function that returns the payload and use it with alias this.
Write a helper struct CheckNull(T), with a member T. The member T is a property that returns NotNull!T and has opCast(T : bool), which checks the payload for null and returns true if it is not null.
Write a helper function, checkNull, which takes a T member and returns CheckNull!T.
Write your functions to accept and return NotNull!T whenever possible.
Optionally, write a generic factory function that returns NotNull!T.
Use the function with if(auto nn = test.checkNull) {...
Change the font size
Change margin width
Change background colour