Difference between revisions of "DIP39"

From D Wiki
Jump to: navigation, search
(Details)
Line 40: Line 40:
 
== Details ==
 
== Details ==
 
Suppose we have a function that takes an input by ref:
 
Suppose we have a function that takes an input by ref:
 +
<syntaxhighlight lang="d">
 
T2 fun(ref T a);
 
T2 fun(ref T a);
We can use it as before with an lvalue x (backwards compatible):
+
</syntaxhighlight>
fun(x);  
+
We can use it as before with an lvalue LV (backwards compatible):
We can use it with an rvalue r and a call site annotation indicating to convert the rvalue to an lvalue via a temporary:
+
<syntaxhighlight lang="d">
 +
fun(LV);  
 +
</syntaxhighlight>
 +
We can use it with an rvalue RV and a call site annotation indicating to convert the rvalue to an lvalue via a temporary:
 
I propose the yet unused symbol '^' to denote this (note, it is used in some C++ extensions but that's irrelevant), although there are alternatives, see section: 'alternative symbols for call site rvalue annotation'.
 
I propose the yet unused symbol '^' to denote this (note, it is used in some C++ extensions but that's irrelevant), although there are alternatives, see section: 'alternative symbols for call site rvalue annotation'.
  
  
fun(r^);  
+
fun(RV^);
 
 
 
 
 
 
  
 
== Alternative symbols for call site rvalue annotation ==
 
== Alternative symbols for call site rvalue annotation ==

Revision as of 00:34, 11 May 2013

DIP 39: Safe rvalue references: compatible with DIP38, backwards compatible, safe against ref/nonref code evolution.

Title: Safe rvalue references: compatible with DIP38, backwards compatible, safe against ref/nonref code evolution.
DIP: 39
Version: 1
Status: Draft
Created: 2013-05-10
Last Modified: 2013-05-10
Author: Timothee Cour
Links:

Abstract

We propose to introduce rvalue references that are:

  • safe: guarantees memory safety so that references will always point to valid memory.
  • compatible with DIP38: can use same inref/outref internal compiler annotation for input references that can be returned by ref by a function.
  • backwards compatible: current valid D code will continue to work without change. In addition, additional code becomes valid with call site rvalue ref annotation.
  • safe against ref/nonref code evolution: call site rvalue ref compulsory annotation turns ref/nonref changes into compile errors instead of silently changing code behavior.
  • both const ref or ref can be used with rvalue refs (more flexible than C++)
  • no call site ref annotation when input ref argument is already an lvalue (different from C#), for backwards compatibility (and making it less verbose)
  • compatible with UFCS

Details

Suppose we have a function that takes an input by ref:

T2 fun(ref T a);

We can use it as before with an lvalue LV (backwards compatible):

fun(LV);

We can use it with an rvalue RV and a call site annotation indicating to convert the rvalue to an lvalue via a temporary: I propose the yet unused symbol '^' to denote this (note, it is used in some C++ extensions but that's irrelevant), although there are alternatives, see section: 'alternative symbols for call site rvalue annotation'.


fun(RV^);

Alternative symbols for call site rvalue annotation

  • fun(r^);//proposed postfix annotation, compatible with left-to-right pipelines in D: [1,2].sort.map!fun.uniq
  • fun(^r);//prefix annotation is compatible with '&' location wrt argument
  • fun(ref r);//reminds of C# call site annotation
  • fun(r@);//# has UDA meaning in D, but that could be made unambiguous
  • fun(r#); //# has a special line reordering meaning in D, but that could be made unambiguous

Copyright

This document has been placed in the Public Domain.