ModuleInfo, Object.localClasses(), and Object.find() - any users?

Arafel er.krali at gmail.com
Tue Dec 13 08:52:48 UTC 2022


On 13/12/22 4:17, Adam D Ruppe wrote:
> Yes, separate compilation is a thing, but you still have to `import` 
> your dependencies.

There's dynamic loading of shared libraries, where you don't even know 
at compilation time what classes there might be coming.

For instance, if you implement a plugin system, and want the classes to 
add themselves automagically to some central registry.

In my view, the best solution for this would be to fix / extend the 
`this` template parameter [1] to every part of the class, in this case 
including static constructors:

```d
struct MyClassInfo { /* ... */ }

MyClassInfo[string] myClassRegistry;

class MyRoot {
	static this(this C) {
		import std.traits : fullyQualifiedName;

		// Gather C's metadata and store it
		myClassRegistry[fullyQualifiedName] = MyClassInfo(/*...*/);
	}
}
```

This way, you could create a hierarchy where everything inheriting from 
`MyRoot` would register automatically.

In an ideal world the current ClassInfo (or even an improved version of 
it) would be part of Object, but not of ProtoObject (what's the status 
of DIP1042, btw?).

[1]: https://issues.dlang.org/show_bug.cgi?id=10488


More information about the Digitalmars-d mailing list