dip1000 rule 5
sclytrack
fake at hotmail.com
Tue Nov 27 08:56:47 UTC 2018
On Monday, 26 November 2018 at 09:10:23 UTC, sclytrack wrote:
> On Sunday, 25 November 2018 at 19:22:36 UTC, sclytrack wrote:
>> There are 4 rules listed.
>>
>> https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md
>>
>>
>>
>>
>> What is rule 5?
>>
>>
>> int* global_ptr;
>>
>> void abc() {
>> scope int* a;
>> int* b;
>> scope int* c = a; // Error, rule 5
>> scope int* d = b; // Ok
>> int* i = a; // Ok, scope is inferred for i
>> global_ptr = d; // Error, lifetime(d) <
>> lifetime(global_ptr)
>> global_ptr = i; // Error, lifetime(i) <
>> lifetime(global_ptr)
>> int* j;
>> global_ptr = j; // Ok, j is not scope
>> }
>>
>
> ---
>
> Are the following assumptions correct?
>
>
> lifetime(a) < lifetime(b)
> Means that b is older and lives longer than a. Or better, the
> data that b is pointing
> to is older and lives longer than the one that a is pointing
> too. With the exception
> of the null pointer which gets unlimited lifetime because it
> does not corrupt memory.
>
> ---
>
> scope int * a;
>
> The variable gets unlimited lifetime because the value it is
> pointing is assigned
> null. And that throws exception when trying to access the
> memory and because
> it does not corrupt memory it is assigned unlimited lifetime.
> Also once a variable
> is assigned unlimited lifetime, then it retains that unlimited
> lifetime during
> the entire reachability of the variable.
>
> scope int * c = a;
works
>
> The above is allowed. You are assigning a variable that
> according to the compiler
> has unlimited lifetime. Therefore the variable c will be
> handled like it has
> unlimited lifetime by the compiler.
> lifetime(c) <= lifetime(a)
> The dip talks about longer and shorter, but is equal okay too?
>
> int * c = a;
works (compiles without error and is inferred scope)
>
> The above can not be inferred because scope is only inferred
> when it is assigned
> a limited lifetime. So it is an error.
Rule number 2 of the DIP1000 between quotes
"2. A variable is inferred to be scope if it is initialized with
a value that has a non-∞ lifetime."
I made an error here. Once scope always scope. And will infer
scope as much as
possible. Rule two is more for local variables that haven't even
been marked
with scope. A pointer to them needs to be inferred scope.
>
> ---
> How is a person able to understand this DIP?
./dmd -betterC -dip1000 test.d
> ---
>
> How many DIP manager are there?
I'll assume single person.
> When is a DIP assigned a number?
>
> ---
More information about the Digitalmars-d-learn
mailing list