DIP1000
Ola Fosheim Grøstad
ola.fosheim.grostad at gmail.com
Wed Jun 29 07:24:19 UTC 2022
On Wednesday, 29 June 2022 at 05:51:26 UTC, bauss wrote:
> Not necessarily, especially if the fields aren't value types.
> You can have stack allocated "objects" with pointers to heap
> allocated memory (heap allocated "objects".)
Those are not fields, those are separate objects… The compiler
knows which is a field (part of the object).
> You can't, or rather you shouldn't have stack allocated fields
> within heap allocated "objects" however; as that will almost be
> guaranteed to lead to problems.
That is perfectly ok if you use RAII and manage life times.
> Ex. from your example then even if the "node struct" you pass
> was allocated on the stack, then the memory the "next" pointer
> points to might not be allocated same place.
>
> Unless I'm misunderstanding what you're trying to say.
You did :). If you look at the post I made in general about
DIP1000 and flow typing you see that I annotate scope with a
number to indicate life time ordering.
If you have `connect(int* a,int* b){a.next = b}` then the
compiler can deduce that the signature with formal parameters
should be `connect(scope!N(int*) a, scope_or_earlier!N(int*) b)`.
The compiler then checks that the actual parameters at the call
site are subtypes (same type or proper subtype).
More information about the Digitalmars-d-learn
mailing list