弃用 : returning this escapes a reference to parameter this

标签 d

在我们的代码库中将 DMD 更新到 2.094.1 时遇到此问题。它是关于什么的以及如何解决它?

Deprecation: returning this escapes a reference to parameter this
       perhaps annotate the parameter with return

警告是针对 return this; 行发出的:

public ref EventBuilder typeOne()
{
    this.type = 1;
    return this;
}

最佳答案

此弃用警告与 DIP25 有关.在此处添加 return:

public ref EventBuilder typeOne() return
{
    this.type = 1;
    return this;
}

引用 DMD 的变更日志 2.092.0 :

DIP25 has been available since v2.067.0, first as its own switch, and more recently under the -preview=dip25 switch. The feature is now fully functional and has been built on, for example by DIP1000.

Starting from this release, code that would trigger errors when -preview=dip25 is passed to the compiler will also trigger a deprecation message without -preview=dip25. The behavior of the switch is unchanged (errors will still be issued).

DIP25 aims to make it impossible for @safe code to refer to destructed object. In practice, functions and methods returning a ref to their parameter might be required to qualify the method or the parameter as return, as hinted by the compiler.

struct Foo
{
    int x;
    // returning `this.x` escapes a reference to parameter `this`,
    // perhaps annotate with `return`
    ref int method() /* return */ { return this.x; }
}
// returning `v` escapes a reference to parameter `v`,
// perhaps annotate with `return`
ref int identity(/* return */ ref int v) { return v; }

In both cases, uncommenting the return annotation will appease the compiler.

关于弃用 : returning this escapes a reference to parameter this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65004175/

相关文章:

d - Nimrod 中类似 Ada 的类型

d - 预编译的 Windows OMF BLAS/LAPACK?

linux - 当主进程突然死亡时,如何杀死 linux spawnProcess?

windows-7 - 如何在Win7和dmd(D2)中使用gtkD?

OS X 上的 Derelict3 D 绑定(bind)

c - 如何在 D 中创建可以与 C 代码接口(interface)的连续多维数组?

d - 你如何在 D 中使用范围?

d - 使用 DUB 管理 C 依赖项

parameter-passing - 如何按值传递对象?

immutability - d2 : immutability of partially known structures