[Dlang Episode 99] D Language - version (for conditional compilation)
Вставка
- Опубліковано 17 чер 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 show you how we do conditional compilation in the D programming language. If you're coming from a C background, then you're probably use to using the preprocessor with #if/#define/#endif and other related ideas. D does not have a preprocessor, because it can compute so much at compile-time, and often this ends up making the language much cleaner. So in this lesson I introduce 'version' which can be used to control conditional compilation, whether for specific features or platforms. I'll show a few examples, and one real world use case where I use this in my code. 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.io/communities/... - Наука та технологія
D'liteful ... on of my favorite programming languages!
D Rocks!
@@MikeShah 'D 🪨🪨🪨'
@@GaryChike 🤣
and another great one!
Cheers!
version(unittest) { ... } comes in handy. I had a problem wherei i needed the fqn of a type and the types defined inside unittest {} weren't in the module namespace. I moved them into version blocks and that solved the issue.
The main use of version for me is in combination with configurations in my dub file. I can then build a specific version by using "dub -c my version" and that sets the version identifiers the build.
@@disdroid Very cool! Thanks for providing a use case here for configuration!
I feel like I Need to watch some C stuff because while I can read it 90 percent of the time now. There is always that 10 percent where my brain doesn't register # is NOT a comment, but a preprocessor directive.
And it does not help that many devs treat their preprocessed commands with # LIKE comments. Like, I just wanna build this library for one place, I don't know how to use the GCC magic incantations all the time computer programmer, help me OUT here lol.
Anyway, back to the video. Just a tiny thing. I treat # internally as a 'directive' in C since I can barely parse otherwise how headers and functional macros...work. Otherwise, I will have a headache.
Good note! Indeed, depending on the compiler there may be more magic with preprocessor directives. As far as getting through this video, the main thing 'version' replaces is the need for:
#if WIN_32
// Do some conditional thing here for windows
#else
// Do something for anything non-windows
#endif
My compiler video might be useful for seeing the preprocessor in action: ua-cam.com/video/ksJ9bdSX5Yo/v-deo.html
cool...
Enjoy!