[question] Access from UDA constructor to parent symbol

crimaniak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Dec 27 02:03:49 PST 2016


On Tuesday, 27 December 2016 at 02:05:27 UTC, Ali Çehreli wrote:
> On 12/26/2016 02:04 PM, crimaniak wrote:
>
>> So my main question: how it is possible to do such thing?
>
> Just to make sure we're on the same page: :)
>
> * There is 'interface' in addition to 'class'
  Yes. I want it for structs exactly.
>
> * If all you want is to check, then you can write template 
> constraints by following the example of std.range.isInputRange.
  Interface implementation has a significant advantage over 
template constraints for implementation break case. With 
interface I have one relevant error on the implementation stage. 
With template constraints I have many less relevant errors on the 
usage stage. It's just different types of contract.

>
> With that aside, what you need is generally achieved by a mixin:
>
> @implements!I1
> struct S1
> {
>     // ...
> }
>
> template ValidateInterfaces() {
>     // Go through all members of the module here
>     // Identify the ones having the 'implements' UDA
>     // The useful tools here are
>     //     __traits(getAttributes)
>     //     __traits(allMembers)
>     //     __traits(getMember)
> }
>
> mixin ValidateInterfaces;

Thanks, this is a way. The only problem user can forget to call 
this mixin. It would be useful to have module-wide compile-time 
variables, in this case it's possible to call ValidateInterfaces 
one time from first @implements instance and user don't have to 
write it at every module.



More information about the Digitalmars-d-learn mailing list