| |
 | Posted by Jonathan M Davis | Permalink Reply |
|
Jonathan M Davis 
| http://d.puremagic.com/issues/show_bug.cgi?id=6148
Summary: Make templates smarter about instantiating with
implicitly convertible arrays
Product: D
Version: unspecified
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: nobody@puremagic.com
ReportedBy: jmdavisProg@gmx.com
--- Comment #0 from Jonathan M Davis <jmdavisProg@gmx.com> 2011-06-11 20:05:35 PDT ---
At present, most of range-based algorithms fail with static arrays and const or immutable dynamic arrays. This is because when a templated function attempts to instantiate them, it uses their _exact_ type, when they can work just fine as long as the appropriate dynamic array is used instead. For instance, find's basic definition for looking for a range within another range looks like this:
R1 find(alias pred = "a == b", R1, R2)(R1 haystack, R2 needle)
if (isForwardRange!R1 && isForwardRange!R2
&& is(typeof(binaryFun!pred(haystack.front, needle.front)) : bool)
&& !isRandomAccessRange!R1)
{...}
static arrays and const or immutable dynamic arrays will fail because they aren't input ranges. However, if find had a special overload for arrays such as
R1 find(alias pred = "a == b", R1, E)(R1 haystack, const(E)[] needle)
if (isForwardRange!R1
&& is(typeof(binaryFun!pred(haystack.front, needle.front)) : bool)
&& !isRandomAccessRange!R1)
{...}
then static arrays and const or immutable dynamic arrays would work, because they would implicitly convert to a dynamic array of const elements of the same type of element that they have. And in fact, as long as the static array isn't const or immutable, it would convert to a mutable dynamic array of the same element type. I would _very_ much like to see the compiler be smart enough to instatiate templates with such dynamic arrays when given static arrays or const or immutable dynamic arrays which would otherwise fail.
I don't know what the best way to handle this is given that templates always instantiate with their exact type at this point, and there _might_ be a case where you'd actually _want_ to instantiate one with a static array or an immutable or const dynamic array. If it's a problem to disallow static arrays and const or immutable arrays as the type that a template instantiates with (since they'd be converted over to the most mutable dynamic array which they could implicitly convert to), then perhaps the solution is to make a second pass through the template instantiation if it fails with a static array or a const or immutable dynamic array and try it with the most mutable array type which they can be implicitly converted to. And then if _that_ works, instantiate it with that type.
Regardless, it would be _highly_ desirable to make it possible for static arrays and const or immutable dynamic arrays to instantiate templates with the most mutable type of array that they will implicitly convert to. Otherwise, the only way to get around the problem is either to overload every range function explicitly for arrays or to print template error messages when someone accidentally uses a static array or a const or immutable dynamic array. Neither of those scenarios is particularly pleasant. These types of arrays _should_ work, but because templates are currently always so exact, even when they don't need to be, they don't work. So, please make it possible for them to instantiate the templates appropriately
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
|