[Dlang Episode 92] D Language - Compile-Time Programming - Part 3 of n - Variadic Template structs
Ғылым және технология
►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 to make use of several of the topics that we have been discussing in regards to compile-time programming. We'll take the idea of a variadic template and apply it to a struct using mixins to generate a struct at compile-time. This is again a very powerful idea, and will lead us into understanding how the AliasSeq concept (shown in the next lesson) enables variadic templats and powerful compile-time sequence programming. As always, share your use cases and cool examples for how you use these tools!
►Please like and subscribe to help the channel!
►KZread Channel: / mikeshah
►Join our free community: courses.mshah.io/communities/...
Пікірлер: 13
I love variadic templates. I might be a little out of depth here since generics/templates still kick me sometimes. but I am ready!
@MikeShah
Ай бұрын
Awesome! D really makes them much cleaner to work with :)
can D call a function a certain number of times in one line?
@MikeShah
Ай бұрын
What did you have in mind? Could use 'repeat' from std.range 1 import std.range; 2 import std.stdio; 3 4 void main(){ 5 6 auto foo(int x) => x*x; 7 writeln(foo(7).repeat(4)); 8 9 }
@thegameoflife9179
Ай бұрын
@@MikeShah thanks, yes, basically i didnt explain why i had asked... i was recently reading the book 'Starting Forth' by Leo Brodie and from page 11 he creates a couple of words (same as functions basically) in which the 1st function (called star) outputs 1 character (a *) and the 2nd function (called stars) calls that 1st function as many times as the number given on the stack, so if we enter: 35 stars It would output 35 of them, etc etc
@MikeShah
Ай бұрын
Ah got it @@thegameoflife9179. This could also be solved recursively if you're printing stars with some input.
@thegameoflife9179
Ай бұрын
@@MikeShah yep, ive no doubt it can be done recursively but i prefer to split into seperate parts like he's done in forth so that they can be reused in other parts of the program... in Common Lisp you could do this... (defun star () (princ '*)) (defun stars (no-of-stars) (loop repeat no-of-stars collect (star))) (stars 35)
An idea: build some kind of ORM...
@MikeShah
Ай бұрын
Building an object relational mapping (ORM) sounds like something that you could indeed use this tool for.
@disdroid
Ай бұрын
I would use the user-defined-attributes for this to make something like spring or EJB in java. on top of that I would use dependency injection and a declarative style to customise the stack at runtime. then you can build the webapp as well as the business code from just a few source files. making use of CTFE removes the requirement for a generational stage during the build.
@bsdooby
Ай бұрын
@@disdroid (User-defined) attributes are another options, yes (however: attributes in D are kind of overused [once w/ @, once w/o @] and not well-defined, IMHO).
@disdroid
Ай бұрын
@@bsdooby they are extremely versatile - I haven't got round to using them as much as I should. the technique is similar to the variadic struct templates - the client attaches meta-data to their type and it gets parsed by CTFE code that can generate some code inside your type. so you would use the attributes and a mixin instantiation inside your class or struct etc. and this would add your object to the ORM. I can use these attributes to specify the database parameters, as well as things like display style for web pages, in the same file as the object defintion. We wouldn't be forced to use these, they'd be the last part of the library to create. we start out using separate configuration files which we parse at compile time, and when the system is operational we create an API so that users can quickly create a runtime in one file and run it using rdmd. so simple!
@disdroid
Ай бұрын
here's another idea - the library can generate a tuple from a given declaration set so we just grab that and use "alias this" in our application. we might not need the mixin in this case.