From D Wiki
Revision as of 14:51, 27 February 2013 by Deadalnix (talk | contribs) (Created page with "{| class="wikitable" !Title: !'''Functions''' |- |DIP: |28 |- |Version: |1 |- |Status: |Draft |- |Created: |2013-02-27 |- |Last Modified: |2013-02-27 |- |Author: |Amaury SÉC...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Title: Functions
DIP: 28
Version: 1
Status: Draft
Created: 2013-02-27
Last Modified: 2013-02-27
Author: Amaury SÉCHET
Links: DIP23, DIP27


This DIP is the second of 3. It intend to define properties in D. Just like DIP27 it intend to be simple and avoid special cases, by defining properties as function that are called 100% of the time in 100% of the situations.


If function are an abstraction for action, properties are abstraction for data. In order to achieve that goal, properties must behave in a way that is as close as possible to data, and avoid cases where it behave in a different way (an error if preferable when 100% compatibility with plain data isn't possible).

A user may want to use a property instead of plain data for the following reasons :

  • To use contract to protect what can be done with actual data.
  • To plug some debug informations or logging to know where the data is accessed/modified.
  • To provide a data like interface to user as it is often nicer to use than setter/getter.
  • To create write only/read only values.

This list is not exhaustive, but is clearly enough to show the need for a data oriented abstraction.

Properties methods

A property is defined as a method with the @property attribute :

strict Foo {
    ReturnType getter() {
        // Function body.

    ReturnType setter(Parameter) {
        // Function body.

Whenever the property is used, it is automatically called. No exception.

Foo bar;
bar.getter;   // getter property is called.
bar.getter(); // Equivalent code if @property wasn't used.

If a property method have a parameter, it is a setter. This property can only be used on the left side of an assignation expression.

Foo bar;
bar.setter = 3; // setter property is called with 3 as parameter.
bar.setter(3);  // Equivalent code if @property wasn't used.

As a property is always called, explicitly calling it is either an error, or a call of the returned value if it is callable.

Foo bar;
bar.getter();   // getter is not callable (if ReturnType isn't callable).
                // Call what is returned by getter otherwise.
bar.getter()(); // Equivalent code if @property wasn't used.
bar.setter(3);  // Error.

As a property is always called, it has the type of its return value.

Foo bar;
static assert(is(typeof(bar.getter) == ReturnType));     // Pass
static assert(is(typeof(bar.getter()) == ReturnType));   // Equivalent code if @property wasn't used.
static assert(is(typeof(bar.setter = 3) == ReturnType)); // Pass
static assert(is(typeof(bar.setter(3)) == ReturnType));  // Equivalent code if @property wasn't used.

As a property is always called, taking its address take the address of the returned value (if allowed).

Foo bar;
&bar.getter; // Error, bar.getter isn't an lvalue.

Properties as free functions

When defined as free function, a property is intended to be used as UFCS. It means that a property must have 1 argument for a getter or 2 arguments for a setter.

ReturnType getter(X a) {
    // Function body.

ReturnType setter(X a, Y b) {
    // Function body.

X a;
Y b;
a.getter; // getter is called.
a.setter = b; // setter is called.

Operator overloading

Assignation can present itself is many shapes. In this case, the expression is rewritten to involve only simple assignation. For instance :

ReturnType prop(X a) {
    // Function body.

ReturnType prop(X a, uint i) {
    // Function body.

X a;

// Become :
{ auto tmp = a.prop; a.prop = tmp + 1; return tmp; }();
{ auto tmp = a.prop(); a.prop(tmp + 1); return tmp; }(); // Equivalent code if @property wasn't used.

a.prop += 42;
// Become :
a.prop = a.prop + 42;
a.prop(a.prop() + 42); // Equivalent code if @property wasn't used.


This DIP propose a data oriented abstraction for D users. It has no special case whatsoever in order to avoid corner cases or bad interactions with other features.


This document has been placed in the Public Domain.