[question] Access from UDA constructor to parent symbol

crimaniak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 26 14:04:26 PST 2016


On Monday, 26 December 2016 at 21:15:03 UTC, Adam D. Ruppe wrote:
> On Monday, 26 December 2016 at 20:07:56 UTC, crimaniak wrote:
>>          // I want to see Foo here and use it's reflection to 
>> iterate fields and methods.
>
> then pass foo to it....
>
> What do you mean parent symbol? I assumed you mean subclass but 
> your example shows one class and one struct. So is it the 
> structure containing the class? Or what?

I mean the character to which the attribute belongs, Foo in this 
case.

>
>
> But, the answer of just passing the argument is probably the 
> best one anyway. You can use a factory function, or pass the 
> type from a constructor to a super method, or something like 
> that.
Let me explain. I want to have struct Foo : BarInterface {}. I 
read forums and found some discussion. Arguments against this 
feature has no sense on my opinion. But in fact I don't think it 
will be implemented. So I try to implement it as library. My idea:

class implements(Interface)
{
	this()
	{
		// iterate all members of Interface and check if this element
		// exists in parent symbol and check if parameters the same.
		// Write clean error message if contract fails.
	}
}

unittest
{
	interface I1
	{
		void foo();
		int bar(int i);
	}


	@implements!I1
	struct S1
	{
		void foo()
		{
			import std.stdio;
			writeln("foo");
		}

		// I want error message like "function int bar(int i) should be 
implemented according to interface I1" in this case
	}
}

So, if I pass both types in parameters it will not be so clean. 
It will be some separate from struct expression, but I want 
attribute like above.

It seems I can find symbol iterating all symbols in __MODULE__but 
in this case it will be O(N^2), there N is amount of @implements 
usages in module.

I also thought about such option:

struct S1
{
	mixin implements!I1;
	...
}

But this option I like less, and I have not researched it.

So my main question: how it is possible to do such thing?




More information about the Digitalmars-d-learn mailing list