template constraint diagnostics

Shammah Chancellor via Digitalmars-d digitalmars-d at puremagic.com
Sat Oct 18 18:58:08 PDT 2014


On 2014-10-18 21:52:30 +0000, Shammah Chancellor said:

> On 2014-10-15 17:29:33 +0000, Trass3r said:
> 
>> http://youtu.be/qwXq5MqY2ZA?t=33m57s
>> 
>> I wish we had diagnostics like that in D.
> 
> I have answered your call:
> 
> https://github.com/D-Programming-Language/phobos/pull/2627
> 
> Please comment.
> 
> -S.

I don't know if anyone looked at this yet, but I've updated it quite a 
bit since the original post.   Please take a look again and comment.   
It also generates some useful DDoc now.

Docs:


bool isConcept(T, C, bool diagnostics = false, string errorPrefix = "")();
Returns true if T supports the concept C. Note, templated member 
functions are not supported currently.

Concepts should be defined as in the following example:
----
class CInputRange(E) : Concept
{
	abstract void popFront();
	@property abstract E front();
	bool empty;

	//Optional Axioms function.  This will be checked if it compiles, and 
that it returns true if it does.
	static bool Axioms(T)()
	{
		return true;
	}
}

class CInfinite() : Concept
{
	static bool Axioms(T)() {
		enum empty = T.empty;
		return !empty;
	}
}

class CInfiniteInputRange(E) : CInputRange!E
{
	mixin CInfinite;
}
---

template conceptDiagnostic(R, string F, int L, C...)
Prints error messages for why template instantiation failed.

Examples:
---
bool DoStuff(R)(R infRange) if ( isConcept!(R, CInfiniteInputRange!string))
{
	return true;
}

bool DoStuff(R)(R infRange) if ( isConcept!(R, COutputRange!string))
{
	return true;
}

//Example of using conceptDiagnostic
bool DoStuff(R, string F = __FILE__, size_t L = __LINE__ )(R infRange)
{
	mixin conceptDiagnostic!(R, F, L, COutputRange!string, 
CInfiniteInputRange!string);
	return false;
}
---

class Concept;
Base class for Concepts. All Concepts should derive from this, or 
another concept.



More information about the Digitalmars-d mailing list