[DLang Episode 110] D Language - Classes - part 4 of N - RAII with structs and classes

  Рет қаралды 233

Mike Shah

Mike Shah

Күн бұрын

►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!
►KZbin Channel: / mikeshah
►Join our free community: courses.mshah....

Пікірлер: 13
@GaryChike
@GaryChike 3 ай бұрын
Thanks again Mike on yet another superb tutorial on D!
@MikeShah
@MikeShah 3 ай бұрын
Cheers! Thanks as always for your support! Got two more episodes queued up too 😃
@bsdooby
@bsdooby 3 ай бұрын
One Mike to rule them all 😊
@MikeShah
@MikeShah 2 ай бұрын
haha :)
@bsdooby
@bsdooby 3 ай бұрын
how are the rules for value types (structs) and (default, or none) ctors?
@MikeShah
@MikeShah 3 ай бұрын
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); }
@bsdooby
@bsdooby 3 ай бұрын
@@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 (?)
@MikeShah
@MikeShah 3 ай бұрын
@@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).
@bsdooby
@bsdooby 3 ай бұрын
@@MikeShah Do you know the rational for this (that empty ctors are forbidden)? Is it to force init. its members to their defaults?
@MikeShah
@MikeShah 3 ай бұрын
@@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.
@bsdooby
@bsdooby 3 ай бұрын
Really want to enjoy this video; but I need time to reflect on the current political situation…
@MikeShah
@MikeShah 3 ай бұрын
Fair!
@wiktorwektor123
@wiktorwektor123 3 ай бұрын
Ok snowflake
The Return of Procedural Programming - Richard Feldman
52:53
ChariotSolutions
Рет қаралды 66 М.
24 Часа в БОУЛИНГЕ !
27:03
A4
Рет қаралды 7 МЛН
Vampire SUCKS Human Energy 🧛🏻‍♂️🪫 (ft. @StevenHe )
0:34
Alan Chikin Chow
Рет қаралды 138 МЛН
#1 Rasmus Hougaard: Human leadership in the age of AI
52:37
Mindful AI Podcast
Рет қаралды 5 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 200 М.
A Deep Dive into JVM Start-Up
22:03
Java
Рет қаралды 32 М.
Dord.
12:57
Vsauce
Рет қаралды 10 МЛН
Таким раствором работать одно удовольствие
1:00
Профессия созидатели
Рет қаралды 954 М.
ЛИТВИН / ПРАНК С ГРИМОМ / Shorts #upx #shorts
0:59
Robot 🤖 cleaning 🧹
0:57
Bunnal 𝚃𝚎𝚌𝚑
Рет қаралды 4,7 МЛН
Американцы красят асфальт?
0:27
BAZAR CLUB
Рет қаралды 188 М.
Pixel 7 и 7 Pro с Face ID - лучше iPhone 14 Pro!
21:12
Rozetked
Рет қаралды 457 М.