[DLang Episode 110] D Language - Classes - part 4 of N - RAII with structs and classes
Вставка
- Опубліковано 18 гру 2024
- ►Full DLang Series Playlist: • D Language (DLang) Pro...
►Find full courses on: courses.mshah.io/
►Join as member to get perks: / @mikeshah
►Lesson Description: In this lesson I talk about RAII -- resource allocation is initialization in the D programming language. I will review how memory is created and freed with structs in D, and then we will look at classes. Overall, there is really only one rule -- just remember that structs that are stack allocated have deterministic destruction. Otherwise, you need to use some abstraction (e.g. scoped, or 'scope(exit) { obj.destroy; }, or call a 'shutdown' member function, to reclaim memory. In this lesson I do a lot of live coding to otherwise demonstrate this concept. As always, share your interesting use cases with the community in the discussion below!
►Please like and subscribe to help the channel!
►UA-cam Channel: / mikeshah
►Join our free community: courses.mshah....
Thanks again Mike on yet another superb tutorial on D!
Cheers! Thanks as always for your support! Got two more episodes queued up too 😃
One Mike to rule them all 😊
haha :)
how are the rules for value types (structs) and (default, or none) ctors?
Structs that are stack allocated (the default way, without using 'new') are value types that will have the constructor called when initialized and the destructor called when they leave scope (in LIFO order). I've posted an example below which requires dmd 2.107 or later for string interpolation (video coming in the future).
import std.stdio;
struct S{
int id;
this(int i){
id=i;
writeln(i"id $(id) created");
}
~this(){
writeln(i"id $(id) destroyed");
}
}
void main(){
S s1 = S(1);
S s2 = S(2);
}
@@MikeShah Nice, that string interp. is now possible.I just did some tests: `T t;` is valid, as is in C++, for stack-allocation. The same for `auto T = T();` , but the parens are needed (?)
@@bsdooby Indeed, 'T t' will just use all the .init values of the member fields. Recall that you cannot have a 'struct constructor' without arguments however (a class however can have empty arguments).
@@MikeShah Do you know the rational for this (that empty ctors are forbidden)? Is it to force init. its members to their defaults?
@@bsdooby In short, D relies on being able to default initialize types without having to run constructor code. So for example, when you create a static or even a dynamic array, you want to make sure every value is initialized to whatever the '.init' value is (e.g. 0 for int, NaN for floats, and usually 'null' for classes). This avoids garbage being in your data -- everything at compile-time can also know the default value (A little more: forum.dlang.org/post/mailman.84.1568924770.8294.digitalmars-d@puremagic.com). For folks like myself coming from C++ this is a slight annoyance in the sense that I often like a default constructor that can run some code -- but on the other hand I understand why it is necessary and perhaps makes it easy to do things like copy structs.
Really want to enjoy this video; but I need time to reflect on the current political situation…
Fair!
Ok snowflake