I've just fixed UFCS for the experimental type function branch

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Sep 11 16:09:47 UTC 2020


On Fri, Sep 11, 2020 at 03:17:52PM +0000, Meta via Digitalmars-d wrote:
> On Friday, 11 September 2020 at 14:54:33 UTC, H. S. Teoh wrote:
[....]
> > To take it a step further: what if you can progressively refine the
> > allowed operations on a template argument T within the template
> > function body?  To use your example of InputRange vs.
> > RandomAccessRange: the function declares it accepts InputRange
> > because that's the most general class. Then within its function
> > body, it tests for RandomAccessRange with a static if, the act of
> > which adds random access range operations on T to the permitted
> > operations within the static if block.  In a different static if
> > block you might test for BidirectionalRange instead, and that would
> > permit BidirectionalRange operations on T within that block (and
> > prohibit RandomAccessRange operations).
> > 
> > This way, you get *both* DbI and static checking of valid operations
> > on template arguments.
[...]
> I think that's exactly what he is talking about with "type-level"
> flow-based typing (kinding? ;-)). If types are just another value,
> though, you don't need separate mechanisms for value-level and
> type-level flow-based typing.

That gives me an idea. What if we have compile-time pseudo-classes that
represent classes? Something like this:

	typeclass InputRange(T) {
		bool empty();
		T front();
		void popFront();
	}

	typeclass BidirectionalRange(T) : InputRange!T {
		T back();
		void popBack();
	}

	auto myTemplateFunc(T, InputRange!T Ir)(Ir r)
	{
		// If successful, this makes `br` an alias of r but with
		// expanded allowed operations, analogous to downcasting
		// classes
		alias br = cast(BidirectionalRange!T) r;

		static if (is(typeof(br)))
		{
			// bidirectional range operations permitted on
			// br
			br.popBack();
		}
		else
		{
			assert(!is(typeof(br)));

			// only input range operations permitted on r
			r.popFront();
		}
	}


T

-- 
It's amazing how careful choice of punctuation can leave you hanging:


More information about the Digitalmars-d mailing list