DIP29

From D Wiki
Revision as of 01:18, 5 March 2013 by WalterBright (talk | contribs)
Jump to: navigation, search
Title: Unique Pointers
DIP: 29
Version: 1
Status: Draft
Created: 2013-02-28
Last Modified: 2013-02-28
Language: D2
Breaks: Nothing, it enables code that doesn't compile at the moment
Links: Discussion on github

Discussion on n.g. More Discussion on n.g.

Abstract

Currently, pointers cannot be converted to and from shared and immutable without an explicit and unsafe cast. Unique pointers can be implicitly cast to and from shared and immutable in safety. A unique pointer can be discovered by examining the expression that generated the pointer.

Rationale

Requiring switching to unsafe code to do routine things like create immutable references is a glaring problem. By recognizing unique pointers much of this can be done safely. More advanced analysis can uncover more cases that can be done safely.

Definition

A UniqueExpression is defined as an expression yielding a pointer that can be implicitly converted to mutable, immutable, or shared.

Expression Case 1: ptr+offset

If an expression ptr+offset yields a pointer, that expression is convertible iff ptr is convertible.

Expression Case 2: ptr-offset

If an expression ptr-offset yields a pointer, that expression is convertible iff ptr is convertible.

Expression Case 3: new T

This is convertible if T is convertible. (Not sure this is completely true.)

A lot more work is needed to enumerate all the cases.

Enhancements

For simplicity, this proposal is restricted to pointers only. It can be enhanced to cover other reference types such as:

  • class references
  • delegates
  • dynamic arrays

It can also be enhanced to allow some non-unique pointers to be converted to shared pointers, if the non-unique references are also shared pointers.

Copyright

This document has been placed in the Public Domain.