Created
June 13, 2025 00:06
-
-
Save crazymonkyyy/577aefe66fda54e838b231434fdbdbf4 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
--- meta.d | |
import std; | |
alias seq(T...)=T; | |
template appendable(discrim...){ | |
enum ptr_=cast(immutable(void)*)[0].ptr; | |
auto poke()=>(*(cast(int*)ptr_))++; | |
auto count()=>(*(cast(int*)ptr_)); | |
template store(int i){ | |
template store(T=void,T[] data=null){ | |
alias store=typeof(data[0]); | |
}} | |
template store(int i,alias A){ | |
alias S=store!i; | |
alias store=S!A; | |
} | |
template append(alias A,discrim...){ | |
alias append=store!(poke(),A); | |
} | |
template opIndex(int i){ | |
static assert(i<count()); | |
alias S=store!i; | |
alias opIndex=S!(); | |
} | |
template get(discrim...){ | |
alias get=seq!(); | |
static foreach(I;0..count()){ | |
get=seq!(get,opIndex!I); | |
}} | |
} | |
--- foo.d | |
import meta; | |
import std; | |
unittest{ | |
alias bar=appendable!(); | |
alias a=bar.append!int; | |
alias b=bar.append!float; | |
alias c=bar.append!bool; | |
static assert(is(bar.opIndex!2==bool)); | |
bar.get!().stringof.writeln; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment