NOT EVEN FIRST DRAFT
About this book
If this is your first book about dlang; don't. This is a collection of rants about template meta-programming. Go read something else first: //todo list
Read this along side or after the template book even if its old as shit //todo link Making this at the earilest the 3rd book you read.
Contributing
Im very open to grammer and spelling edits, not so with tone and opinion policing. Geust chapters plz.
Me
//todo crop
Im monkyyy, the grumpy asshole whos chornicaly unempoyed and seemly unable to finish a solo project. //todo
Thoery
whats a template anyway
//todo make allot more verbose and clear, and get beta readers who dont understand templates
In cooking theres a difference between a techique and a recipe. A Betty Crocker yellow cake (americain edition)
reads differently from a description of a stirfry
. Likewise Stepanov ranting about math is different from plain old c.
https://youtu.be/KbC4jvGPFs0?si=8OdMfCIJAMAnUAKI&t=155 //todo look up video embeding //todo consider making my own cut/upload
//todo summerize with d code translations
Where c is very close to thin layer over a finished program and is therefore like repeating a packaged cake recipe, templates are where Stepanov can put his ideas as a techique.
You dont need to have betty crooker and her yellow number 7, her alumium powder, too hold your hand to stirfry pork and leeks; it will be fine, pinkie promise. Likewise functions like swap
don't need to be complex things, they just need a little gap where a type can go. You dont need a "class that extends swapable" or some other dynamatic type thoery with strong opinions, you need a hole and a bit of text replacement. Where T
can be a int
or float
, the meat of your choice
could be pork
or beef
.
template stirfry(alias Meat, Vegibles...)
and stirfry!(pork,leek)
; you replace specail tokens to generate your recipe on the fly. Which is fine because pork
and beef
are culiarily very simuliar, handling the list of vegibles may take some getting used to, to say nothing of how to express "seasoning". But all of that is possible.
//todo consider extending examples for vegetables and seasoning
However take note, a meat of your choosing
is wildly underspecified; stirfry of crocodile
is technically a phase that has syntactic meaning but ancient china probably did not define the seasoning profile for a Floridian delicacy. You have two choices, either a) Refuse to work for all choices or b) let it play out and let the user beware. Unlike a cookbook, an api can be opinionated and controlling, I think this is a mistake, the goal should be to attempt to compile everything. Let the user have their food. It will be less work and have more product.
Bad turing-completeness
//todo rant about how "reductions" in computer science are often "expansions", a round trip conversion from sat to minesweeper will be much much larger
In the fundmentals of computer science is the concept of a "reduction", a turing machine can simulate lamda calculous, lamda calculous can emulate addition, and lamda calculous can run a turing machine emulator.
It should be noted this is a terrible name; a "reduction" is a drastic expansion to compute. It important for non-constructive math if a machine exists that you cant prove halts, its less so for you throwing together a 10 line script.
Lisp doesnt do chruch numerals, they use op code; so even that crowd has limits to the level of impractically they will tolerate. I believe a chruch numeral muliplication is O(nm^2) to actaully compute, while if you squint and ignore the most autist of programmers, muliplcation of ints is const of just a few cycles.
Templates for a genertic programmer can do anything, because it has enough chruch-ness to impliment the text replacement turing compleness thoery. BUT thats a truely awful method of computation. I believe D is far in the lead for simplifying and actaully compiling templates. Template metaprogramming is a chruch machine that generates turing machines. Even the safetyphiles will praise alias reassignment
for being a massive speed up, its insanity it took so long it seems to me to be something like not requiring chruch numerals in lisp, before any list in template land would be of O(n^2) complexity before you even started. This book will cover imparitive template computation, these are bugs, but remember, how a chruch machine emulates a turing machine.
syntax sugar has semantic meaning
New-ish features
The template book is older then 10 years old; while D devolpment was slow, there was some progress. This section should serve as a extention to that book.
Alias reassignment
//todo since this was added after the template book, some coverage somewhere is nessery
//todo enum reassignment pattern
//todo upstream bug vs opends fix
openD string imports
//some example code idk
Other patterns
Specailization as lookup tables
int foo(T:int)()=>1;
int foo(T:float)()=>2;
Walters wild ride
//todo find walters talking about dmd not dealllocating
//todo find quote of probaly snar complaining that dmd live edits ast
Given that the compiler doesnt deallocate and it live edits some bugs are natural imparitive features
Useful Bugs/ugliness
Monkyyy header
(T,T data)
doesnt correctly reset
tg's counter
enum counter=cast(immutable(void)*)[0].ptr;
auto getcount()=>(*(cast(int*)counter));
auto count()=>(*(cast(int*)counter))++;
unintentional Features
Mixin counter
My old compile time counter, its worse
mixin("__LINE__")
with a -mixin=mix
flag