Thread overview
DIP1046, "ref For Variable Declarations", has been Accepted
Jun 24
Mike Shah
June 23

DIP1046, "ref For Variable Declarations", has been accepted:

https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1046.md

Under the new DIP process, any DIP written by one of the language maintainers has an extra step to go through once it's ready to leave the DIP Development forum and enter into Formal Assessment: it must first be discussed at a DLF meeting to determine if there are any major objections that should block its approval. If there are none, then the other language maintainer is free to review the DIP and render a final verdict. Otherwise, further discussion and debate is undertaken to determine how to proceed.

Since Walter is the author of DIP1046, I presented it at a meeting for which the following members were present:

Jonathan M. Davis
Timon Gehr
Martin Kinkelin
Dennis Korpel
Mathias Lang
Mike Parker
Robert Schadek
Steven Schveighoffer
Adam Wilson

None of the members raised any objections to block its approval. I then submitted it to Átila for review. He requested a clarification, which Walter made, and then approved it. He agreed with the DIP's rationale that ref declarations are a major tool for writing memory-safe code and believes they align with our overarching goal of enhancing memory safety in D.

In contrast, Walter's bitfields DIP, currently in its third draft, was presented at the same meeting and received major objections. I'll have a report on how we decided to proceed when I post the update for that meeting.

June 23

On Sunday, 23 June 2024 at 13:29:21 UTC, Mike Parker wrote:

>

DIP1046, "ref For Variable Declarations", has been accepted:

https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1046.md

>

which reduces the complexity of the code, as long-winded global names get a shorthand

Does ref add anything compared to using an alias?..

June 23
On Sunday, June 23, 2024 3:50:37 PM MDT Vladimir Marchevsky via Digitalmars-d- announce wrote:
> On Sunday, 23 June 2024 at 13:29:21 UTC, Mike Parker wrote:
> > DIP1046, "ref For Variable Declarations", has been accepted:
> >
> > https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1046.md
> >
> > which reduces the complexity of the code, as long-winded global names get a shorthand
>
> Does ref add anything compared to using an alias?..

alias allows you to give a symbol another name (essentially resulting in the compiler replacing the new name with the original name anywhere that it sees it), whereas ref involves taking the address of a variable - just like a pointer except that it's automatically dereferenced for you, and since the compiler controls the creation of ref (vs being able to take the address of pretty much anything to create a pointer), it can be @safe as a result. So, you can think of ref as being an @safe pointer (and what can be done with it is more restricted in order to be able to guarantee that it's @safe). This DIP expands the use of ref so that there are more places where we can use it, whereas before, those cases would have required pointers and likely would have required @trusted on code that took the address to create the pointer in order for the code to be @safe.

https://dlang.org/spec/declaration.html#alias

https://dlang.org/spec/declaration.html#ref-storage

While alias can be used on symbols such as variables, it's usually used on types (similar to C/C++'s typedef), whereas ref can only be used on variables.

- Jonathan M Davis



June 24

On Sunday, 23 June 2024 at 21:50:37 UTC, Vladimir Marchevsky wrote:

>

On Sunday, 23 June 2024 at 13:29:21 UTC, Mike Parker wrote:

>

DIP1046, "ref For Variable Declarations", has been accepted:

https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1046.md

>

which reduces the complexity of the code, as long-winded global names get a shorthand

Does ref add anything compared to using an alias?..

ref will bind to lvalue expressions, e.g (from the DIP):

ref int dark(ref int x, int i, int* q)
{
    ref m = *q;    // m refers to whatever q points to
    ...
}

*q is an expression, not a symbol. ref can bind to e.g. a struct field runtime value, or an lvalue returned from a function.

alias only allows giving new names for existing compile-time symbols.

June 24

On Monday, 24 June 2024 at 11:08:32 UTC, Nick Treleaven wrote:

>

On Sunday, 23 June 2024 at 21:50:37 UTC, Vladimir Marchevsky wrote:

>

On Sunday, 23 June 2024 at 13:29:21 UTC, Mike Parker wrote:

>

[...]

>

[...]

Does ref add anything compared to using an alias?..

ref will bind to lvalue expressions, e.g (from the DIP):

ref int dark(ref int x, int i, int* q)
{
    ref m = *q;    // m refers to whatever q points to
    ...
}

*q is an expression, not a symbol. ref can bind to e.g. a struct field runtime value, or an lvalue returned from a function.

alias only allows giving new names for existing compile-time symbols.

This is welcoming news! A nice addition for folks transitioning over from C++ who are use to having this functionality.