hot reloading template specailizations

monkyyy crazymonkyyy at gmail.com
Thu Jun 26 06:22:01 UTC 2025


I have my hotreloading lib and went to use it on some templates 
code; stockily it broke upon contract with templates(I half 
expected it to break)

I planed on having an overload set of maybe 256 "tiles"
```d
int tile(int I:0)(float x,float y){
	return 0;
}
```

I defined
`int temptile(float x,float y)=>tile!(lastindex!tile)(x,y);`
it didnt update
while a `return 1;` did

I assume this is something popping out of the fundamental order 
of operations of how mangling works; these are fundamentally c 
libs and c doesnt have templates. My d code is just magic glue. 
Templates inverse the causality of how headers are made. And it 
may just be impossible to get something working.

Given overload set `foo` where im in the process of defining 
`foo!0..` upwards, how do I hotreload the newest version of 
`foo!(lastindex!foo)` *as Im adding more and more*; or does 
anyone want to confirm its functionally impossible given my 
appooch.

---
foot note, irrelevant but for completeness:
```d
template lastindex(alias T,int start=0){
	static if(__traits(compiles,T!(start+1))){
		alias lastindex=lastindex!(T,start+1);
	} else {
		enum lastindex=start;
}}
```
current hotreload:
the changes from my hotloading gist are about trying to pull out 
some more speed I dont think this effects templates in anyway:

```d
mixin template hotloadImport(string file,alias 
mark="hotload",Duration iolimit=dur!"msecs"(500)){
	import std;
	import core.stdc.stdlib;
	import core.sys.posix.dlfcn;
	template impl(string _:file){
		mixin("import "~file~";");
		alias 
functions=Filter!(isCallable,getSymbolsByUDA!(mixin(file),mark));
		static assert(functions.length!=0,"no functions detected, add a 
`@\"hotload\":`");
		bool considercompiling(){
			static SysTime lastio, lastmodified;
			return maxwatch(lastio,Clock.currTime(),iolimit) && 
maxwatch(lastmodified,(file~".d").timeLastModified,dur!"msecs"(0));
		}
		enum filepath=PATH~file~".so";
		template hotload(alias F){
		auto hotload(T...)(T args){
			static void* handle;//copyed from outside scope, what was I 
thinking before?
			static typeof(&F) call;
			static bool hasrun=false;
			if(considercompiling){
				if(hasrun){dlclose(handle);}
				hasrun=true;
				compile(file);
				handle=dlopen(&filepath[0],RTLD_NOW);
				assert(handle!=null,dlerror.to!string);
				call=cast(typeof(&F)) dlsym(handle,F.mangleof);
			}
			//scope(exit) dlclose(handle);//does this do anything?
			return call(args);
		}}
	}
	static foreach(A;impl!file.functions){
		pragma(msg,Stringof!A);
		mixin("alias "~Stringof!A~"=impl!file.hotload!A;");
	}
}
```


More information about the Digitalmars-d-learn mailing list