Thread overview
Not able to load classes defined in archive file in Mac OSX using Object.factory
Nov 16, 2018
Aditya
Nov 16, 2018
Adam D. Ruppe
Nov 16, 2018
Adam D. Ruppe
Nov 16, 2018
Aditya
Nov 16, 2018
Basile B.
Nov 16, 2018
Adam D. Ruppe
Nov 16, 2018
Aditya
November 16, 2018
Hello !

I am trying to work out "The D programming Language" chapter 1.6 problem on Interfaces and Classes.

folder
./stats.d    (takes argument "Min/Max" on command line and loads them)
./ss/Stat.d  (defines interface named 'Stat')
./ss/Min.d   (defines class named 'Min' that implements Stat)

$ dmd -lib -g ss/*.d -of=ss.a
$ dmd -g stats.d ss.a
$ echo 3 5 1.3 4 10 4.5 1 5 | stats Min
object.Exception@stats.d(19): Invalid statistics function: Min

Code for stats.d -

import std.exception, std.stdio;
import ss.Stat;  // works !!

void main(string[] args){
	Stat[] stats;

	foreach (arg; args[1 .. $]) {

		// Doesn't work
		// string path = "stats." ~ arg ~ "." ~ arg;
		// string path = "stats." ~ arg;
		// string path = "stats.ss." ~ arg;
		// string path = "stats.ss." ~ arg ~ "." ~ arg;
		// string path = "ss." ~ arg;
		// string path = "ss." ~ arg ~ "." ~ arg;
		// string path = arg ~ "." ~ arg;
		// string path = arg;
		// string path = "stats.ss." ~ arg ~ "." ~ arg;

                 string path = arg;

		auto newStat = cast(Stat) Object.factory(path);
		enforce(newStat, "Invalid statistics function: " ~ path);
		stats ~= newStat;
	}
}

What is the path for Min class defined in Min.d inside ./ss folder that is compiled into ss.a ?

Thanks
Aditya

November 16, 2018
On Friday, 16 November 2018 at 14:24:10 UTC, Aditya wrote:
> ./ss/Stat.d  (defines interface named 'Stat')
> ./ss/Min.d   (defines class named 'Min' that implements Stat)

Did you put a module declaration at the top of those files? Like

module ss.Stat;


That ought to be required; the compiler lets you skip it, but it doesn't work reliably without it once you start importing the module or other similar things.



But once you have that, the name of your class will be the full name with module, so like if min has

module ss.Min;

class Min {}


the full class name will be

ss.Min.Min

the full module name dot the full class name.
November 16, 2018
PS object.factory sucks and I hope it is removed some day. There's no plan to do so, but I still wouldn't actually rely on it... instead, I'd write your own factory function and registration system, so you control it and will have reliability to your specific needs.

but still if the factory works for you, eh, you can use it.
November 16, 2018
On Friday, 16 November 2018 at 14:28:22 UTC, Adam D. Ruppe wrote:
> On Friday, 16 November 2018 at 14:24:10 UTC, Aditya wrote:
>> ./ss/Stat.d  (defines interface named 'Stat')
>> ./ss/Min.d   (defines class named 'Min' that implements Stat)
>
> Did you put a module declaration at the top of those files? Like
>
> module ss.Stat;
>
>
> That ought to be required; the compiler lets you skip it, but it doesn't work reliably without it once you start importing the module or other similar things.
>
>
>
> But once you have that, the name of your class will be the full name with module, so like if min has
>
> module ss.Min;
>
> class Min {}
>
>
> the full class name will be
>
> ss.Min.Min
>
> the full module name dot the full class name.

Module Names were not added. But after adding the same, still same result.

// Following still doesn't work
ss.Min.Min
ss.ss.Min
ss.ss.Min.Min
stats.ss.Min.Min
stats.Min
November 16, 2018
On Friday, 16 November 2018 at 14:30:02 UTC, Adam D. Ruppe wrote:
> PS object.factory sucks and I hope it is removed some day. There's no plan to do so, but I still wouldn't actually rely on it... instead, I'd write your own factory function and registration system, so you control it and will have reliability to your specific needs.
>
> but still if the factory works for you, eh, you can use it.

How can one write own factory function ? Any pointers to this ?

Thanx
November 16, 2018
On 11/16/18 9:51 AM, Aditya wrote:
> On Friday, 16 November 2018 at 14:28:22 UTC, Adam D. Ruppe wrote:
>> On Friday, 16 November 2018 at 14:24:10 UTC, Aditya wrote:
>>> ./ss/Stat.d  (defines interface named 'Stat')
>>> ./ss/Min.d   (defines class named 'Min' that implements Stat)
>>
>> Did you put a module declaration at the top of those files? Like
>>
>> module ss.Stat;
>>
>>
>> That ought to be required; the compiler lets you skip it, but it doesn't work reliably without it once you start importing the module or other similar things.
>>
>>
>>
>> But once you have that, the name of your class will be the full name with module, so like if min has
>>
>> module ss.Min;
>>
>> class Min {}
>>
>>
>> the full class name will be
>>
>> ss.Min.Min
>>
>> the full module name dot the full class name.
> 
> Module Names were not added. But after adding the same, still same result.
> 
> // Following still doesn't work
> ss.Min.Min
> ss.ss.Min
> ss.ss.Min.Min
> stats.ss.Min.Min
> stats.Min

So to let you know, Object.factory is not a maintained function. The ability to instantiate classes based on the ModuleInfo was overruled by the fact that we don't want to generate ModuleInfo if we don't have to, and it should be as small as possible. Therefore, you need some triggers to make sure the module info is included, and even then, I think, it's not guaranteed to include the classinfo in that moduleinfo.

The better path is to use a registration system to add the desired classes for reflection. Not a trivial thing, but that's probably the path you want. I'd suggest looking at serialization libraries like orange (http://code.dlang.org/packages/orange) or cereald (http://code.dlang.org/packages/cerealed) for ideas, or maybe just use one of them.

-Steve
November 16, 2018
On Friday, 16 November 2018 at 14:55:52 UTC, Aditya wrote:
> On Friday, 16 November 2018 at 14:30:02 UTC, Adam D. Ruppe wrote:
>> PS object.factory sucks and I hope it is removed some day. There's no plan to do so, but I still wouldn't actually rely on it... instead, I'd write your own factory function and registration system, so you control it and will have reliability to your specific needs.
>>
>> but still if the factory works for you, eh, you can use it.
>
> How can one write own factory function ? Any pointers to this ?
>
> Thanx

for learning purpose you can make very simply a factory function based on string comparison.

Object simpleFactory(string className)
{
   import the_module_where_stuff_is_declared;
   import the_module_where_thing_is_declared;

   if (className == "Stuff")
       return new Stuff;
   else if if (className == "Thing")
      return new Thing;
   // and so on
   else return null;
}

although this requires imports so the command line for building should include more path using the "-I" cmd line switch.

I propose this solution because making a true factory might be too complicated if you just start learning D.
November 16, 2018
On Friday, 16 November 2018 at 14:55:52 UTC, Aditya wrote:
> How can one write own factory function ? Any pointers to this ?

Something like

Interface delegate()[string] factories;

Interface create(string className) {
    if(className in factories)
       return factories[classname];
    else
       return null; // or throw if you prefer
}



In every module in which you create a class, you will want to add code like this:

class Class : Interface {}

// register with the factory in a module constructor
static this() {
   factories["Class"] = Interface delegate() {
       return new Class();
   }
}


And it should work.