DIP1046, "ref For Variable Declarations", has been Accepted

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Jun 24 03:37:46 UTC 2024


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





More information about the Digitalmars-d-announce mailing list