Labelled @nogc Working With Inner Scopes
Benoît Dubreuil
b.dubreuil at hotmail.com
Mon Aug 30 20:26:46 UTC 2021
I've got the following module `dummy.dummy.d`:
```d
module dummy;
@safe:
@nogc:
struct Dummy
{
// TODO: Why do I need to repeat those? Aren't the formers
still effective for inner scopes?
@safe:
@nogc:
static const int MAX_MEMBER_INT = 3;
int memberInt;
string memberStr;
this(int memberInt, string memberStr) nothrow
in
{
assert(memberInt > MAX_MEMBER_INT);
}
out
{
assert(this.memberInt == memberInt);
assert(this.memberStr == memberStr);
}
do
{
this.memberInt = memberInt;
this.memberStr = memberStr;
}
int doStuff() pure nothrow
{
auto result = this.memberInt - 1;
if (result <= MAX_MEMBER_INT)
result = 1 + MAX_MEMBER_INT;
return result;
}
}
```
I've also got the following module `dummy.test.d`:
```d
module test;
@safe:
@nogc:
private:
version (unittest)
{
import dummy;
/// Test [cg.math.dummy.dummy.Dummy.doStuff()]
unittest
{
Dummy instance = Dummy(4, "A dummy string");
assert(instance.doStuff() > Dummy.MAX_MEMBER_INT);
}
}
```
My question is:
> In the source file `dummy.dummy.d`, why the first labelled
> attributes `@safe` and `@nogc` have no effect inside the
> struct's scope? In other words, why do I need to repeat
> labelled attributes statements in order to affect inner scopes?
To test this, simply remove in `dummy.dummy.d` the *TODO* comment
and its following labelled attributes.
More information about the Digitalmars-d-learn
mailing list