Optional tags and attributes

Stanislav Blinov stanislav.blinov at gmail.com
Fri Jan 17 18:35:42 PST 2014


On Saturday, 18 January 2014 at 01:52:56 UTC, Rikki Cattermole 
wrote:

> Okay, I'll explain what I was inferring. If the methods your 
> calling are lets say nothrow which can be checked by a pure 
> function lets say. Using the template if statement we can check 
> that it does throw.

I'm going to continue to think that I am overly not explicit and 
thus there continues to be a misunderstanding. However, I'd like 
to correct that misunderstanding by saying that *I KNOW HOW TO DO 
THIS WITH TEMPLATES AND STATIC IF* :D (By the way, I did sort of 
mention it in the first post). In other words, I wasn't asking 
*how* to do it in principle, I'm interested if there's any 
possibility to do it elegantly.

> For example:
> void myfunc(T)(T arg) nothrow if (checkIfNothrow!T) {}
> void myfunc(T)(T arg) if (checkIfNoModifiers!T) {}
> void myfunc(T)(T arg) pure if (checkIfPure!T) {}
>
> Basically you have to define all combinations. That is what I 
> was meaning.
> Preferably you'll use q{} your code that is actually used. And 
> use a template mixin to generate all these statements, so you 
> don't have to!

That is precisely the thing I'd like to avoid entirely. Following 
your example (which is not entirely correct since in this case I 
won't be checking T, I'll be checking T.foo or T.bar or whatever 
combination of those :o), I'd have to have three distinct 
function definitions, even though in the end only one of them 
will ever be compiled into my class. Yes, this can be simplified 
by mixing-in the actual function body into all of those 
definitions. But we have to be able to do better :)

I'm not trying to say that C++'s syntax wins, no. The syntax can 
be anything, other than blowing up my class with all those 
declarations and ifs :). If we're talking templates, it would 
sure be nice to be just a tad more generic.

Consider:

void myfunc(T)(T arg) pure(isPure!(T.foo)) 
nothrow(isNoThrow!(T.bar)) { ... }

I know that myfunc *can* be pure (I wrote it) and it *won't* 
throw anywhere, and that would be true for all the function body, 
*except* I also know that T.foo *may* be impure or T.bar *may* 
throw (because I didn't write them, T is an arbitrary type 
possibly from user code). Because of those "may"s I either have 
to drop the tags entirely, or to have four different declarations 
with various combinations of constraints. Having the ability to 
specify the condition for a tag would elegantly solve this issue. 
It's one definition, no static ifs or additional template 
constraints. Again, the syntax here could be anything, like 
something proposed in that discussion I mentioned earlier:

void myfunc(T)(T arg) @optional(isPure!(T.foo), pure) 
@optional(isNoThrow!(T.bar), nothrow) { ... }

or anything else. Granted, presence of overloads and templates 
may make those ifPure/ifNoThrow/ifWhatever checks not that 
trivial to implement or invoke, but still possible nevertheless.


More information about the Digitalmars-d mailing list