Compile-Time Interfaces (Concepts)

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 23 08:26:54 PDT 2014


On Wed, Jul 23, 2014 at 04:46:20PM +0200, bossfong via Digitalmars-d-learn wrote:
> Am 23.07.2014 16:27, schrieb H. S. Teoh via Digitalmars-d-learn:
> >
> >We could, though it's not quite the same as a native concepts
> >implementation where the compiler can check templates for code that
> >wrongly makes assumptions about the incoming type that aren't defined by
> >the concept.
> >
> >
> >T
> >
> true.
> 
> Still, maybe compiler errors could be provided by a library that
> defines an "Concept(Interface)" UDA that you could use to annotate
> implementations of "Concepts"?
> 
> import concepts;
> 
> @Concept(InputRange)
> struct MyInputRange
> {
> // ...
> }
> 
> verifyConcepts();
> 
> Is it that what you mean?
> 
> Just thinking things through here...

No, I'm talking about catching errors like this:

	auto myRangeAlgo(R)(R range)
		if (isInputRange!R)
	{
		...
		auto r = range.save;	// <--- oops
		...
		return Result(...);
	}

	unittest
	{
		auto testData = [1,2,3];
		auto result = myRangeAlgo(testData);

		// Test will pass, because arrays are also forward
		// ranges, which have a .save method. So we fail to
		// catch the bug in the code above.
		assert(result.equal(expectedResults));
	}


T

-- 
Bomb technician: If I'm running, try to keep up.


More information about the Digitalmars-d-learn mailing list