Skip to content

Protonull/DullSafety

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DullSafety

This is a source-library package that attempts to approximate null-restricted types in D. It's somewhat hilarious to me that D's expansive feature set allows such approximations, but still hasn't managed to natively support null-restricted types. It's honestly kind of impressive.

How to use

You use this by prefixing a null-assignable type with NonNull! (eg: NonNull!string/NonNull!(int*)). You should limit your usage of this to the "fringes" of your APIs, ie, function parameters and return types.

Converting from a type into its NonNull! equivalent is as easy as using .asNonNull(), eg:

void exampleFunction(NonNull!string message);
exampleFunction("Hello, World!".asNonNull());

Unwrapping happens automatically, eg:

NonNull!string exampleFunction();
string result = exampleFunction(); // string has been automatically unwrapped from `NonNull!string`

For APIs that do not [wish to] use NonNull!, this package comes with a convenience assertion method:

string exampleThirdPartyFunction();
string result = exampleThirdPartyFunction().assertNonNull();

Warnings and advisories

You should avoid using NonNull! for variable types unless they are const or immutable.

You should also NEVER create a default-initialised or void-initialised NonNull!, as while this is valid D code, it violates the contract of NonNull!. You should ALWAYS use .asNonNull(), no exceptions.

You may run into issues with the automatic unboxing, in which case you should use the .unwrap() method.

Licence

This project is licensed under Apache 2.0.

Purpose

The ultimate purpose of this package is to exasperate the Dlang team into adding native support for null-restricted types.

About

Approximate null-safety for Dlang

Topics

Resources

License

Stars

Watchers

Forks

Languages