Created
May 7, 2025 21:18
-
-
Save crazymonkyyy/6600a4bb3937e73715093430f7dfb103 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template mkytemp(T,T[] fakedata=null){//my favorate bug | |
alias mkytemp=typeof(fakedata[0]); | |
} | |
unittest{ //verifing the bug still exists | |
static assert(is(mkytemp!int==int)); | |
static assert(is(mkytemp!float==int));// <- spooky | |
} | |
/*the ast is modified live, so the T[] is defined and doesnt get properly "unset", | |
null and typeof just glue to make it work nicely; the effect is in the header at the callsite | |
DO NOT REPORT | |
unsafe to access as it writes a value, but this is the only way i know to "side effect"-y store a *type* /alias | |
*/ | |
//--- | |
enum __counter=cast(immutable(void)*)[0].ptr;//tg's ct counter, uglified for namespace | |
auto __getcount()=>(*(cast(int*)__counter)); | |
auto __count()=>(*(cast(int*)__counter))++; | |
unittest{//does it all just werk? | |
static assert(__getcount==0); | |
static assert(__count==0); | |
static assert(__getcount==1); | |
static assert(__count<__getcount); | |
} | |
//--- | |
//modifcation of tgs counter to be a "writeonce" that unlike mkytemp can be safely accessed | |
enum writeonceint=cast(immutable(void)*)[int.min].ptr; | |
bool writeonceisempty()=>(*(cast(int*)writeonceint))==int.min; | |
int writeonce(int i){ | |
if(writeonceisempty){ | |
(*(cast(int*)writeonceint))=i; | |
} | |
return (*(cast(int*)writeonceint)); | |
} | |
unittest{ | |
static assert(writeonceisempty); | |
static assert(writeonce(420)==420); | |
static assert(! writeonceisempty); | |
static assert(writeonce(1337)==420); | |
} | |
//--- | |
/* On memoization: | |
Auto memoized templates is the correct defualt behavior, but as a trade off like eager and lazy: | |
you need the ability to turn it off | |
I use `(discrim...)`, and treat it as the call sites responibly to fill the `discrimiation` to unmemoize template code; this has usablity tradeoffs, there is another option of __LINE__ in the header but I think thats best saved for one step before the user. | |
Lazy, eager, memoized, "landmine" `foo()(){static assert(0)}`; with out of order: parsing, ct, ctfe, and then finally run time. Its a big ol` mess to understand how the compiler works to biuld a program, once you start breaking assumptions you may need to start caring. Have FUN | |
Someone should make a table with example code | |
*/ | |
//--- copy from here | |
template writeablealias(discrim...){ | |
enum _state{empty,written,read}; | |
template mkytemp(T,T[] fakedata=null){ | |
alias mkytemp=typeof(fakedata[0]); | |
} | |
enum __state=cast(immutable(void)*)[_state.empty].ptr; | |
auto state(){ | |
assert(__ctfe); | |
return (*(cast(_state*)__state)); | |
} | |
void set(T)(){ | |
static assert(state==_state.empty,"writeable alias w/ "~discrim.stringof~" was attempted to be written to with "~T.stringof~" while in state:"~( | |
state==_state.written ?"written":"read" | |
)); | |
alias S=mkytemp!T; | |
enum E=(){//ctfe-ify | |
(*(cast(_state*)__state))=_state.written;return 1; | |
}(); | |
} | |
template read(){// call as read!() | |
static assert(state==_state.written,"writeable alias w/ "~discrim.stringof~" was read before written"); | |
alias read=mkytemp!(void); | |
} | |
//alias writeablealias=read; //possible terseness but Im hoping non-wizards understand | |
template read(alias Default){ | |
static if(state==_state.written){ | |
alias read=mkytemp!(void); | |
} else { | |
enum E=(){(*(cast(_state*)__state))=_state.read; return 1;}(); | |
alias read=Default; | |
}} | |
} | |
//dont copy unittests, spookiness | |
unittest{//alias usage | |
alias foo=writeablealias!"foo"; | |
foo.set!int; | |
foo.set!int;//works due to automemoization | |
//foo.set!float; fails with nice error | |
static assert(is(foo.read!()==int)); | |
} | |
unittest{//discrimatied usage | |
static assert(is(writeablealias!"foo".read!()==int)); | |
static assert(is(writeablealias!"foo".read!(float)==int)); | |
} | |
unittest{//out of order parsing is possible | |
alias array()=writeablealias!"array".read!()[5]; | |
array!() makearray()(){ | |
array!() o; | |
return o; | |
} | |
struct myint{int i;} | |
writeablealias!"array".set!myint; | |
auto foo=makearray;//resolves here | |
static assert(is(typeof(foo)==myint[5])); | |
} | |
unittest{ | |
static assert(is(writeablealias!3.read!(int)==int)); | |
} | |
unittest{ | |
//alias three=writeablealias!3.set!float; fails | |
static assert(is(writeablealias!3.read!(float)==float));//you can have super position of defualts | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment