Skip to content

Instantly share code, notes, and snippets.

What's Safety?

In the D spec, and in this document, safety refers to an absence of undefined behavior. safe language features and safe functions cannot lead to undefined behavior as long they're combined with other safe features. It's implied in the definition that safety also means memory safety. Which means no memory corruption.

In contrast, unsafe features and functions are those that can lead to undefined behavior if used incorrectly. In particular, they might corrupt memory.

For example, dereferencing a pointer is considered safe. In actuality, dereferencing a pointer can have undefined behavior, if the pointer is invalid. And writing through such a pointer can certainly corrupt memory. In contrast, creating an invalid pointer doesn't have undefined behavior. But dereferencing is the more common and useful operation, so it has been decided that dereferencing is safe while features that might create invalid pointers are unsafe.

The Safety Attributes

import std.conv : text;
import std.exception : enforce;
static import std.file;
import std.math : FloatingPointControl;
import std.meta : AliasSeq;
import std.path : setExtension;
import std.process : execute;
void main()
{
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import std.array: replicate;
import std.conv;
import std.datetime;
import std.stdio;
void main()
{
enum size = 1024 * 1024 * 10; /* 10 MB */
go("a", size);
go("ä", size);
import std.algorithm;
import std.conv;
import std.file;
import std.range;
import std.stdio;
import std.regex;
void main()
{
string[] versions;