User defined safety?
jdrewsen
jdrewsen at nospam.com
Tue Aug 30 10:41:41 PDT 2011
Den 26-08-2011 14:38, Adam Ruppe skrev:
> My pet feature request could do this too. User defined attributes
> combined with a list of functions called by a function.
>
> ===
>
> @custom("mysafe") void foo() {}
> void bar() {}
>
> @custom("mysafe") void main() {
> foo();
> bar();
> }
>
> CheckCustomSafety!("mysafe", main);
>
> template CheckCustomSafety(string attribute, alias func) {
> static if(!__traits(hasCustomAttribute(func, attribute))
> pragma(error, func.stringof ~ " is not " ~ attribute);
>
> foreach(f; __traits(getCalledFunctions, func))
> CheckCustomSafety!(attribute, f);
> }
>
> ====
Couldn't this be used for marking classes as serializable. Something
like this:
class Foo {
@custom("serialize") Cake theCake;
Bar cache;
}
SerializeCustom!("serialize", Foo);
template SerializeCustom(string attribute, alias cls) {
foreach (m; __traits(listMembers, cls)) {
static if ( __traits(hasCustomAttribute(attribute, m) ) {
Serializer.register(cls, m);
}
}
}
----------
Though I would prefer if you could just write:
@custom("serialize") class Foo { ... }
instead of:
SerializeCustom!("serialize", Foo);
/Jonas
More information about the Digitalmars-d
mailing list