December 11, 2022
On Friday, 9 December 2022 at 22:20:28 UTC, Walter Bright wrote:
>
> 4. Could we just remove it entirely?

Yes, please.
December 11, 2022
On Friday, 9 December 2022 at 22:20:28 UTC, Walter Bright wrote:
> Currently, the ModuleInfo struct generated for each module also includes a pointer to the ClassInfo for every class in that module.
>
> The only reference to this data is in the functions Object.localClasses() and Object.find(). The idea is to be able to instantiate a class via a text string rather than a link to the name.
>
> This adds a lot of extra size.
>
> 1. Does anybody use Object.localClasses() or Object.find() ?
>
> 2. Does anybody need them?
>
> 3. Could it be changed to only include the classes marked `export` ?
>
> 4. Could we just remove it entirely?



I vote for 4
December 12, 2022

On Friday, 9 December 2022 at 22:20:28 UTC, Walter Bright wrote:

>

Currently, the ModuleInfo struct generated for each module also includes a pointer to the ClassInfo for every class in that module.

The only reference to this data is in the functions Object.localClasses() and Object.find(). The idea is to be able to instantiate a class via a text string rather than a link to the name.

This adds a lot of extra size.

  1. Does anybody use Object.localClasses() or Object.find() ?

  2. Does anybody need them?

  3. Could it be changed to only include the classes marked export ?

  4. Could we just remove it entirely?

This would affect Object.factory, yes? I've used that previously in commercial software.
Can partially work around it with std.traits.moduleName and __traits(allMembers, somemodule), but what other options are there for finding classes outside of the current module other than those explicitly referenced in the calling module?

December 12, 2022

On 12/12/22 3:40 PM, cc wrote:

>

This would affect Object.factory, yes?  I've used that previously in commercial software.
Can partially work around it with std.traits.moduleName and __traits(allMembers, somemodule), but what other options are there for finding classes outside of the current module other than those explicitly referenced in the calling module?

You need to register those classes with a system, to replace the functionality that ModuleInfo and Object.factory provide.

But it would be better to do that anyway, since the compiler isn't completely consistent with which classes it inserts into ModuleInfo.

-Steve

December 12, 2022
On 12/12/2022 12:40 PM, cc wrote:
> This would affect Object.factory, yes?

Yes.

> I've used that previously in commercial software.

I'm curious as to the reason it was being used.

> Can partially work around it with `std.traits.moduleName` and `__traits(allMembers, somemodule)`, but what other options are there for finding classes outside of the current module other than those explicitly referenced in the calling module?

One option is for the code that is "publishing" a class to be used by Object.factory is to simply make a function in that code that can be called that returns an array of classinfo's for the published classes.
December 12, 2022

On Monday, 12 December 2022 at 20:58:40 UTC, Walter Bright wrote:

>

I'm curious as to the reason it was being used.

This was an older project ported from Objective-C that read external definition data, looking at it now it can (and probably should) be replaced. The allowed class list still needed to be verified so it's the same issue of explicitly registering classes, so disregard that example.

In newer stuff I have RPC and serialization/remote object duplication libraries where I explicitly register modules to scan for classes by UDA (or have them indirectly register their own modules when one class calls in to the library). It would be nice to be able to preemptively scan/register classes in arbitrary modules without needing to list them in a second area of code. One (minor) example I have is a toString(OutputRange)() promoter that calls the correct templated function when the variable type doesn't match the typeid. This doesn't use ModuleInfo, but it could. If derived classes exist across multiple files, it can miss registering some of them if they call out of order. Otherwise I need to explicitly register each class I want handled here, which is the same "make one change in multiple places" problem that avoiding is one of my favorite strengths of D.

tl;dr I can live without it, but now that I'm reminded it exists, it seems like a nice thing to have. 😕

December 12, 2022

On Monday, 12 December 2022 at 21:49:41 UTC, cc wrote:

>

It would be nice to be able to preemptively scan/register classes in arbitrary modules without needing to list them in a second area of code.

What I like to do is have classes register themselves. You can do a mixin RegisterThis; thing that you import from the lib. The downside is if you forget to register a child, it won't remind you until you try to use it. But the upside is you can do all kinds of custom work in there, it avoids scanning modules, and can be done without a central list.

The RegisterThis thing adds a static constructor that appends the factory to the runtime list.

Would be nice if druntime offered some thing like this to replace the existing Object.factory - you can migrate by like mixin imported!"core.factory".Register; to make it possible. Then an extra would-be-even-nicer is if there was some way so all objects that inherit from a parent or implement a particular interface either 1) reminded you to register it or 2) just self-registered automatically. (You can kinda do this with the curiously-recurring template pattern but still subclasses can fall through the cracks.)

December 12, 2022
On Mon, Dec 12, 2022 at 09:49:41PM +0000, cc via Digitalmars-d wrote: [...]
> It would be nice to be able to preemptively scan/register classes in arbitrary modules without needing to list them in a second area of code.
[...]

The way I did it in one project was to tag each struct (I was using structs instead of classes, but the same principle applies) with a UDA, and then import all modules that contain the struct definitions, and use getSymbolsByUDA to retrieve all the corresponding symbols. Using this mechanism, I didn't have to manually maintain a list of structs to include; the code automatically picked up the complete list. Of course, modulo importing all the modules that contain the definitions.

Of course, having classes self-register also works too.


T

-- 
MASM = Mana Ada Sistem, Man!
December 12, 2022

On 12/12/22 5:01 PM, Adam D Ruppe wrote:

>

On Monday, 12 December 2022 at 21:49:41 UTC, cc wrote:

>

It would be nice to be able to preemptively scan/register classes in arbitrary modules without needing to list them in a second area of code.

What I like to do is have classes register themselves. You can do a mixin RegisterThis; thing that you import from the lib. The downside is if you forget to register a child, it won't remind you until you try to use it. But the upside is you can do all kinds of custom work in there, it avoids scanning modules, and can be done without a central list.

The RegisterThis thing adds a static constructor that appends the factory to the runtime list.

Would be nice if druntime offered some thing like this to replace the existing Object.factory - you can migrate by like mixin imported!"core.factory".Register; to make it possible. Then an extra would-be-even-nicer is if there was some way so all objects that inherit from a parent or implement a particular interface either 1) reminded you to register it or 2) just self-registered automatically. (You can kinda do this with the curiously-recurring template pattern but still subclasses can fall through the cracks.)

The downside there is if you run into cycles. Clearly, this isn't going to contribute to a cycle, but there's no way to specify that it won't.

A way to address cycle problems would be nice here. On one hand, you have to depend on a module that initializes the registry, so you depend on the static ctor sorting to work, but on the other hand, there aren't going to be any cycles for this particular thing. I'm not sure of the right answer, and I've thought about it a lot.

-Steve

December 13, 2022
On Tuesday, 13 December 2022 at 01:39:43 UTC, Steven Schveighoffer wrote:
> A way to address cycle problems would be nice here.

Right. I think we'd need to redesign ModuleInfo a little here... and I actually think the dependency tree can be figured out at compile time. Yes, separate compilation is a thing, but you still have to `import` your dependencies.

but idk i gotta get to bed, maybe i'll see about writing more tomorrow. You're right it isn't especially easy, but if we are changing ModuleInfo maybe this sis the opportunity to redefine things to make it at least doable.