User:Quickfur/DIP scope

From D Wiki
Jump to: navigation, search
Title: DIP Proposed design for "scope" implementation
DIP: ???
Version: 1.9
Status: Draft
Created: 2014-7-10
Last Modified: 2014-7-10
Author: H. S. Teoh
Links: ...

Abstract

The goal of this DIP is to work out a concrete design for the implementation of 'scope'.

Rationale

The 'scope' keyword has been around for years, yet it is barely implemented and it's unclear just what it's supposed to mean. This DIP hopes to work out concrete details on the semantics of 'scope' so that it actually has a chance of being implemented.

Description

This DIP proposes the following:

  • scope can be applied to any type in the language, and becomes part of the type. Thus, scope(int) is a distinct type from int, for example. We shall call these types scoped types. They interact with the current types in the language via a set of rules, which will be defined below.
  • Every scoped type is associated with a lifetime. For convenience, we may regard non-scoped types as a special case of scoped types, where the associated lifetime is infinite. The lifetime of a scoped type is inherited from the scope in which it is declared, and is regarded as part of its type. Thus, a scope(int) declared in one lexical scope is considered to be a different type than a scope(int) declared in a different lexical scope (even though they will, in general, be compatible with each other according to the rules described in the remainder of this DIP). Exactly what lifetime is assigned to a scoped type is defined in the following section.

Lifetimes

Rules

This section defines the rules of interaction between scoped types and non-scoped types.

The underlying thrust of all of these rules may be termed the Law of Narrowing Lifetimes, that is, one can only retain or reduce the lifetime of a scoped type, never expand it. The one exception to this law is that value types can be copied from a type of narrower lifetime to a type of a wider lifetime, because semantically, a copy of the value is being made, hence we do not violate the lifetime of the original variable that held the value.

Usage

Copyright

This document has been placed in the Public Domain.