[dmd-internals] Type matching / type deduction with pointers

Michel Fortin michel.fortin at michelf.com
Wed Dec 29 08:13:46 PST 2010


I was looking at improving my patch for the "const(Object)ref" syntax so it works correctly with type matching and type deduction. To get a feel of how things should work, I built a few test cases with pointers with the intent to use them as a reference for how it should behave with classes. 

**The problem is that the results for pointers don't make any sense.**

First, type matching. All the static asserts below passes. That doesn't look normal.

	// Type matching

	template cp(T : const(int)*) {
	    enum cp = 1;
	}

	static assert(cp!(int*));
	static assert(cp!(const(int*)));
	static assert(cp!(const(int)*));
	static assert(cp!(immutable(int*)));
	static assert(cp!(immutable(int)*));
	static assert(cp!(shared(int*)));
	static assert(cp!(shared(int)*));

I'd expect at least the 'shared' ones to fail, and also those where the pointer is not mutable.

Second, type deduction. All of these pragma(msg) print "int", except the last one where the template doesn't match.

	// Type deduction

	template ep(T : const(U)*, U) {
	    alias U ep;
	}

	pragma(msg, ep!(int*).stringof);            // int
	pragma(msg, ep!(const(int)*).stringof);     // int
	pragma(msg, ep!(const(int*)).stringof);     // int
	pragma(msg, ep!(immutable(int*)).stringof); // int
	pragma(msg, ep!(immutable(int)*).stringof); // int
	pragma(msg, ep!(shared(int*)).stringof);    // int
	pragma(msg, ep!(shared(int)*).stringof);    // error: no match

I'd have expected both tests with 'shared' to fail, as well as those where the pointer is not mutable.

How am I supposed to make things work correctly for class references if it doesn't even work for pointers? :-)

That said, with what I have now, type matching and type deduction seem to work equally well for class references and pointers (perhaps even a little better for class references). So I'm leaning on soon posting a new patch with what I have.

And I should probably post the above as a separate bug.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/





More information about the dmd-internals mailing list